How To Give Priority For A Regex Pattern Over Another
I am using regular expressions to extract university names. Mainly two patterns are observed. 'some name' university --> ex: Anna University university of 'something' --> e
Solution 1:
In general, alternations in regular expressions are evaluated from left to right, so the leftmost alternatives are checked first, giving them priority. You already did that, though - the reason why you still got the match from the right side of the alternation is that that match is possible earlier in the string.
Therefore you need to be more specific and only allow a "Foo University"
match only if no of
follows. You can use a negative lookahead assertion for this:
regex = re.compile('|'.join([r'university of (\w+){1,3}',
r'(?:\S+\s){1,3}\S*university(?!\s+of\b)']),
flags=re.I)
Post a Comment for "How To Give Priority For A Regex Pattern Over Another"