Search code examples
pythonregexpython-re

Match strings that might include a number


I would like to match strings that may or may not have 1 or more numbers.

Test Data:

    110>=abcdef12
    abcdef12>=110
    110>=1332abcdef
    1442abcdef>=110
    110>=abcdef
    abcdef>=110

Desired Matches:

abcdef12
abcdef12
1332abcdef
1442abcdef
abcdef
abcdef

I have tried [a-zA-Z_\d*]+ but this also matches the 110 on each line. Is there a way to exclude just numbers?

I am using python though I believe this to be irrelevant.


Solution

  • Looking at your sample data, I would do it following way:

    import re
    data = '''110>=abcdef12
    abcdef12>=110
    110>=1332abcdef
    1442abcdef>=110
    110>=abcdef
    abcdef>=110'''
    found = re.findall(r'[a-zA-Z\d]*[a-zA-Z][a-zA-Z\d]*', data)
    print(found)
    

    output

    ['abcdef12', 'abcdef12', '1332abcdef', '1442abcdef', 'abcdef', 'abcdef']
    

    Note that I did not put _ in pattern, as I do not know if _ is digit for you or _ is letter for you, so you would need to adjust it accordingly.