Python: Regex outputs 12_34 - I need 1234

Posted by Guy F-W on Stack Overflow See other posts from Stack Overflow or by Guy F-W
Published on 2012-06-03T10:38:30Z Indexed on 2012/06/03 10:40 UTC
Read the original article Hit count: 185

Filed under:
|

So I have input coming in like: 12_34 5_6_8_2 4___3 1234

and the output I need from it is:

1234, 5682, 43, 1234

I'm currently working with r'[0-9]+[0-9_]*'.replace('_','') which (as far as I can tell) successfully rejects any input which is not a combination of numeric digits and under-scores, where the underscore cannot be the first character. However, replacing the _ with the empty string causes 12_34 to come out as 12 and 34.

Is there a better method than 'replace' for this? Or could I adapt my regex to deal with this problem?

© Stack Overflow or respective owner

Related posts about python

Related posts about regex