multiple subpatterns matches

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
explodec14
Forum Newbie
Posts: 1
Joined: Tue Sep 14, 2010 9:13 am

multiple subpatterns matches

Post by explodec14 »

Hi to all -
I have the string "apple banana lemon melon".
I have applied on it the following regex for matching:

Code: Select all

/apple banana| banana lemon|lemon melon/ims
I would like to accept at the end of the process 3 matches:
[*]apple banana
[*]banana lemon
[*]lemon melon

when i use preg_match_all/preg_match i get only the first subpattern that matches.

How can i get all of the 3 subpatterns ?

10x in advance
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: multiple subpatterns matches

Post by ridgerunner »

The preg_match_all function begins searching at the character position immediately following the previous match. Thus, any matches in your text which overlap after the first (the second two in your given example), will not be found.

You'll have to run each of the three subpatterns separately.
Post Reply