Search code examples
pythonregexnumbers

regex expression to match multiple digits on a single line


Trying to get multiple digits even if its on the same line.

this is what I have so far

import re

lines = '''

1 2

3

4

G3434343 '''

x = re.findall('^\d{1,2}',lines,re.MULTILINE)
print(x)

the output I am getting is:

[1,3,4]

The output I want to get is:

[1,2,3,4]

not sure what to try next any ideas ? Keep in mind the numbers can also be two digits here's another input example

8 9
11

15

G54444

the output for the above should be [8,9,11,15]


Solution

  • You use the ^ which anchors the regex to the beginning of the line. Remove that to find e.g., the "2".

    Then you'll also get the "34"s from the "G3434343" which you don't seem to want. So you need to tell the regex that you need to have a word boundary in front and after the digit(s) by using \b. (Adjust this as needed, it's not clear from your examples how you decide which digits you want.)

    However, that \b gets interpreted as a backspace, and nothing matches. So you need to change the string to a raw string with a leading 'r' or escape all backslashes with another backslash.

    import re
    
    lines = '''
    
    1 2
    
    3
    
    4
    
    G3434343 '''
    
    x = re.findall(r'\b\d{1,2}\b', lines, re.MULTILINE)
    print(x)
    

    This prints:

    ['1', '2', '3', '4']