1

How to write regex for string like:

'$12.78..'

I googled and found "re" is the Python module to do it using import re.

But I couldn't figured out the exact way to do it.

6
  • The exact language of regex's you are trying to parse is also unclear. Commented Sep 10, 2011 at 9:11
  • import re re.match(^$.) Commented Sep 10, 2011 at 9:15
  • 1
    do not forget to escape the dollar sign, as it matches the end of line. Commented Sep 10, 2011 at 9:22
  • naming a variable "tuple" is not a good idea as it is a Python type. And re.match returns None or a match object not a tuple, so the variable name is misleading. Commented Sep 10, 2011 at 9:25
  • s = '$12.78..' re.match('\$(\d*)\.(\d*)',s) Commented Sep 10, 2011 at 9:25

1 Answer 1

5

As a starter:

 import re
 print re.match("\$\d\d\.\d+", "$12.1")
 print re.match("\$\d\d\.\d+", "12.1")
 print re.match("\$\d\d\.\d+", "$123.1")
 print re.match("\$\d\d\.\d+", "$12.")

If you want to learn more about regular expressions in Python I recommend:

Sign up to request clarification or add additional context in comments.

2 Comments

also a google class video about regular expressions: youtube.com/watch?v=kWyoYtvJpe4
@Stals: thanks for the hint, did not know that. For other readers: youtube.com/watch?v=kWyoYtvJpe4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.