CodeNoob
CodeNoob

Reputation: 1840

non-greedy matches greedy?

There is probably a really simple explanation but I just don't see it right now... I have this regex:

(\s.+?\sstress)

I want it to match something like [SPACE]some word[SPACE]stress. However it matches to much:

This will cause a lot of work stress 

will match: will cause a lot of work stress
But .+? should be non-greedy so I expected it to only match work stress.
Click here to open this in regex101.

Upvotes: 2

Views: 254

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626729

.*? is non-greedy, but the regex engine works from left to right, and the first \s matches the left-most whitespace, and . can match any char, and thus, although it is lazily quantified, it must get to the whitespace followed with stress substring.

To just get work stress, use

\s(\S+\sstress)

or just

\S+\s+stress

See the regex demo.

The main point here it to exclude whitespace matching between the first \s and the second \s in the regex. \S+ matches one or more non-whitespace symbols and is a more restrictive pattern compared to ..

Upvotes: 2

Related Questions