What doesn't work in this number matching regular expression?
Question: I am running Python 2.7 and I am currently having some issue with [RE
library](https://docs.python.org/2/library/re.html). 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 <](http://i.stack.imgur.com/GtSmn.png)

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
    


Answer: 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 `\`.

