2

I am not familiar with Regex. Actually, I've just started with it. I have three different patterns

pattern = re.compile(r'SETUP...\d+')
pattern = re.compile(r'PRO...\d+')
pattern = re.compile(r'INSTALL...\d+')

Some of my strings are SETUP1234, SETUP = 1234, SETUP 1234, SETUP-1234 etc. The same with the others. So, I decide that 3 characters between the prefix and the numbers is a reasonable way to use it. But my problem now is, can I combine the three of them in one Regex instead of calling three different findall?

1 Answer 1

4

You can use | like this

pattern = re.compile(r'(SETUP|PRO|INSTALL)...\d+')

it means that any of SETUP, PRO or INSTALL.

Also, the pattern can be improved a little, like this

pattern = re.compile(r'(SETUP|PRO|INSTALL).{1,3}\d+')

This allows 1 to 3 characters to be used in between the words and the numbers.

As Tim suggested in the comments, you can use non-capturing group like this

pattern = re.compile(r'(?:SETUP|PRO|INSTALL).{1,3}\d+')
print pattern.findall("SETUP 1234")
# ['SETUP 1234']
print pattern.findall("PRO 1234")
# ['PRO 1234']
print pattern.findall("INSTALL 1234")
# ['INSTALL 1234']
Sign up to request clarification or add additional context in comments.

3 Comments

If you want the non-capturing version, you can make it (?:SETUP|PRO|INSTALL)...\d+
This return only the first part of the pattern (eg SETUP)
@Sfinos Please check the updated answer, non-capturing version will give the entire string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.