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

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -