Art
Art

Reputation: 69

Why does Regexps find only one match, when there're actually more?

If the text for the experiment is "aaa" and the regexp pattern "(a)+" - why do I get only one match: "aaa"?

Why don't I get:

Should I keep on mind some imaginary cursor that having found one match - moves to the end of it, and the new search goes on from that new position?

It has nothing to do with the so called lazy quantificators, if I'm not mistaken.

If I use "(a)+?" instead of the first pattern, I get 3 matches, which is actually what you expect. But it has nothing to do with what I described before.

Is it possible to get all the straight forward occurrences?

Upvotes: 2

Views: 107

Answers (2)

Szymon
Szymon

Reputation: 43023

Finding matches in a string usually works this way in most (if not all) languages. It starts matching at the beginning of a string. It tries to match against the regular expression and moves the "cursor" as the matching characters are found. Once the match is found, it starts again from the next character, it doesn't move the "cursor" back.

Also note that there are 2 types of matches for quantifiers (+ is one of the quantifiers). Greedy will match as many characters as possible (aaa in your example) and lazy as few as possible (a in your example). But the match of aa is not possible - it's neither greedy nor lazy.

Getting all the matches you'd like to see may be possible using some programming languages constructs other then regular expressions. You would need to also limit match's boundaries to achieve your not-greedy-nor-lazy match.

Upvotes: 1

Craig Estey
Craig Estey

Reputation: 33601

This is called a "greedy" match. It's how a [your's] regex works.

(a)+ says [in effect], if you find an a [+ means "one or more"], keep appending any abutting as until you don't find an [abutting] a--then [and only then] present the match. Hence, the "greediness".

To get what you want/expect will require a more complex regex, possibly inside a loop inside a perl/awk/python/etc. script.

That is, you do the initial match and then using a loop, output all the things you want from the match text.

Upvotes: 0

Related Questions