Match RE till next occurrence of RE

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

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Match RE till next occurrence of RE

Post by anjanesh »

Code: Select all

preg_match_all('#abc.*?#i', "abcdeABCDEFGHIJKabcdefg", $matches);
print_r($matches);
How do I get

Code: Select all

Array
(
    [0] => Array
        (
            [0] => abcde
            [1] => ABCDEFGHIJ
            [2] => abcdefg
        )
 
)
 
?

Problem with using '#abc.*?abc#i' is that it'll skip alternate ones
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: Match RE till next occurrence of RE

Post by GeertDD »

Code: Select all

 
preg_match_all('#[a-z]+|[A-Z]+#', 'abcdeABCDEFGHIJKabcdefg', $matches);
 
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Re: Match RE till next occurrence of RE

Post by anjanesh »

GeertDD wrote:

Code: Select all

 
preg_match_all('#[a-z]+|[A-Z]+#', 'abcdeABCDEFGHIJKabcdefg', $matches);
 
That doesnt solve it. It returns

Code: Select all

Array
(
    [0] => Array
        (
            [0] => abcdeABCDEFGHIJKabcdefg
        )
 
)
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: Match RE till next occurrence of RE

Post by GeertDD »

That's not what it returns over here. You didn't add the i modifier, I hope?

Code: Select all

 
preg_match_all('#[a-z]+|[A-Z]+#', 'abcdeABCDEFGHIJKabcdefg', $matches);
print_r($matches);
 

Code: Select all

 
Array
(
    [0] => Array
        (
            [0] => abcde
            [1] => ABCDEFGHIJK
            [2] => abcdefg
        )
 
)
 
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Re: Match RE till next occurrence of RE

Post by anjanesh »

Oh...I did have the i modifier in my RegExp.
But - Im trying to find all matches to return the string until it reaches the next match.
Your solution wont work for the string "abcdeabcdefghijkabcdefg"
preg_match_all('#[a-z]+|[A-Z]+#', "abcdeabcdefghijkabcdefg", $matches);
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: Match RE till next occurrence of RE

Post by GeertDD »

You mean like this?

Code: Select all

 
preg_match_all('#abc.*?(?=abc|$)#i', 'abcdeABCDEFGHIJKabcdefgabc', $matches);
print_r($matches);
 

Code: Select all

 
Array
(
    [0] => Array
        (
            [0] => abcde
            [1] => ABCDEFGHIJK
            [2] => abcdefg
            [3] => abc
        )
 
)
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Re: Match RE till next occurrence of RE

Post by anjanesh »

Ah..thanks - this is new (?=).
How come I missed this pattern syntax in the doc !
Thanks
Post Reply