I’m trying to match any of the following lines with a regex in python:
RAA RAA
RAA RAA / OOO OOO
RAA RAA / OOO OOO / ROCKY
These strings should always be on their own line so RAA RAA moves over there. wouldn’t match.
I came up with this regex using RegExr:
^([A-Z]*([ ]?)*([A-Z]?)*([ \/]?)*)*$
This works fine to match all the different lines however it causes python to hang if it tries to match RAA RAA moves over there.
I’ve no idea why. Are there any regex experts that might have some insight?
Your entire pattern is full of optional matches, which is likely causing lots of backtracking, and thus the hanging experience. Try using a mandatory match where it makes sense, such as:
A cleaner pattern, without the unnecessary capturing groups, would be:
Notice that the use of
+instead of*ensures that at least one character must match, rather than making the entire pattern optional and taxing the regex engine.