1

im starting to learn regex and i tried to make a simple regex that doesn't make sense to match but it matched any way i tried in python

import re


pattern = r'[a-z]+[a-z]'



print re.findall(pattern,"adasdasad");

it returned ['adasdasad'] where it shouldn't have worked because [a-z]+ should have consumed the whole string and the rest ([a-z]) won't find any thing to conusme why it evaluates ????

1 Answers1

5

The + is not a possessive quantifier and allows backtracking into the quantified subpattern.

The [a-z]+ matches adasdasa and [a-z] matches d, see this demo.

enter image description here

BTW, if you used [a-z]++[a-z] with PCRE (a pattern with a possessive quantifier ++), it would never match anything as it would require to match 1 or more letters and then another letter that would be already consumed with the first subpattern. So, that is the same as (?!) pattern.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • thank you can you give more info about a reference to learn more about these things – user3809210 Sep 05 '16 at 20:40
  • What exact topic are you interested in? In general, I can suggest doing all lessons at [regexone.com](http://regexone.com/), reading through [regular-expressions.info](http://www.regular-expressions.info), [regex SO tag description](http://stackoverflow.com/tags/regex/info) (with many other links to great online resources), and the community SO post called [What does the regex mean](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean). A [rexegg.com](http://rexegg.com) is also a great site. – Wiktor Stribiżew Sep 05 '16 at 20:42