0

I used a non-greedy(lazy) +? operator but it is being greedy

I expect the RE \S+?@\S+ when matched against the string 'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008' to match 'd@uct.ac.za', but it matched 'stephen.marquard@uct.ac.za'. Below is the Python code I used.

import re
string = 'From stephen.marquard@uct.ac.za Sat Jan  5 09:14:16 2008'
out = re.findall('\S+?@\S+', string)
print(out)

When I tried the same string and pattern in Notepad++, I got the same result.

  • 1
    https://stackoverflow.com/questions/27385942/why-does-a-simple-non-greedy-regex-greedily-include-additional-characters-be and https://stackoverflow.com/questions/23176140/regex-lazy-vs-greedy-confusion – azro May 30 '23 at 21:15
  • Your example has nothing to do with greedy versus non-greedy, in part because there is only one `'@'` in the example string. Regardless of whether you used `\S+?@\S+` or `\S+@\S+` the match would be the last string of non-whitespace characters preceding `'@'`, then `'@'`, then the string of non-whitespace characters following `'@'`. – Cary Swoveland May 30 '23 at 21:15
  • 1
    To highlight what's going on, you can see use a capture group `(\S+?)@\S` vs `(\S+)@\S` on something that has two `@` symbols. You'll see that the non-greedy version will capture up to the first `@` but the greedy one will capture up to the second `@`. As shown in the answers posted by azro, regex is only looking left-to-right and not retroactively looking to shorten the matches. – Michael Cao May 30 '23 at 21:17
  • @Michael, always test! [non-greedy](https://regex101.com/r/E87g5R/1) and [greedy](https://regex101.com/r/ONbPZt/1). :-) – Cary Swoveland May 30 '23 at 21:26
  • 1
    I agree with the decision to close the question but this question is not a duplicate of the earlier question cited, as this question is in fact unrelated to whether a part of the regex is greedy or not. – Cary Swoveland May 30 '23 at 21:35
  • If, for some reason, you actually want the result `d@uct.ac.za`, all you need to do is remove the quantifier from the first part `\S@\S+` – CAustin May 30 '23 at 22:24

0 Answers0