regex - why doesn't python regular expression alternation (A|B) match as per doc? -
when run below statement in python 2.7,
re.search('eagle|cat', 'the cat animal. eagle bird').group() i'm expecting see 'eagle' result per regular expression doc i'm getting 'cat'. missing here?
a regular expression alternative patterns (separated |) not scan whole string first alternative, second.
instead, each alternative considered @ each position in input string. @ position 0, neither eagle nor cat match, @ position 4, cat matches, though eagle tried first.
thus, cat returned match; rest of string no longer needs considered.
the alternative ordering matters when both patterns match at same location. cat|cats return cat, always, if there s after word in input string:
>>> import re >>> re.search('cat|cats', 'like herding cats.').group() 'cat' >>> re.search('cats|cat', 'like herding cats.').group() 'cats'
Comments
Post a Comment