简体   繁体   中英

What doesn't work in this number matching regular expression?

I am running Python 2.7 and I am currently having some issue with RE library . As a simple example, when trying numbers on a human written document, I wrote this regular expression :

import re
number = r'^[(]?([-]?([ ]?\d[ ]?)+)|([-]?([ ]?\d[ ]?)+[.,]([ ]?\d[ ]?)*)[)]?$'

This is supposed to match numbers with comas, dots and blanks because humans don't always format it well, and with or without - , ( and ) .

Even if they are not optimized, I expected the following expressions to work, but they obviously don't as we can see on the screenshot.

ex1 = r'^[(]?([-]?([ ]?\d[ ]?)+)|([-]?([ ]?\d[ ]?)+[.,]([ ]?\d[ ]?)*)[)]?$'
ex2 = r'^(\()?(-)?( ?\d ?)*[.,]?( ?\d ?)*(\))?$'
ex3 = r'^[(]?(-)?( ?\d ?)*[.,]?( ?\d ?)*[)]?$'

> Screenshot to output <

Input :

import re

print "1st example :"
number = r'^[(]?([-]?([ ]?\d[ ]?)+)|([-]?([ ]?\d[ ]?)+[.,]([ ]?\d[ ]?)*)[)]?$'
print re.match(number, "16 juillet 1993")

print "\n2nd example :"
number = r'^(\()?(-)?( ?\d ?)*[.,]?( ?\d ?)*(\))?$'
print re.match(number, "16 juillet 1993")
print re.match(number, "161993")
print re.match(number, "-1619,93")
print re.match(number, "-( 9 9 . 3 )")

print "\n3rd example :"
number = r'^[(]?(-)?( ?\d ?)*[.,]?( ?\d ?)*[)]?$'
print re.match(number, "-( 9 9 . 3 )")

Ouput :

1st example :
<_sre.SRE_Match object at 0x103da5480>

2nd example :
None
<_sre.SRE_Match object at 0x103da5480>
<_sre.SRE_Match object at 0x103da5480>
None

3rd example :
None

Try: number=r'^-?\\(?( ?\\d ?)+[.,]?( ?\\d ?)*\\)?$' here, - is out side of ( ) . If you want match - inside of ( ) , please use: number=r'^\\(?-?( ?\\d ?)+[.,]?( ?\\d ?)*\\)?$'

If you want match parentheses ( ) in string, you should escape it by \\ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM