Python: Regex outputs 12_34 - I need 1234
- by Guy F-W
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?