Page 1 of 1
Match RE till next occurrence of RE
Posted: Sun Feb 10, 2008 8:05 am
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
Re: Match RE till next occurrence of RE
Posted: Sun Feb 10, 2008 11:11 am
by GeertDD
Code: Select all
preg_match_all('#[a-z]+|[A-Z]+#', 'abcdeABCDEFGHIJKabcdefg', $matches);
Re: Match RE till next occurrence of RE
Posted: Sun Feb 10, 2008 9:19 pm
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
)
)
Re: Match RE till next occurrence of RE
Posted: Mon Feb 11, 2008 2:16 am
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
)
)
Re: Match RE till next occurrence of RE
Posted: Mon Feb 11, 2008 2:24 am
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);
Re: Match RE till next occurrence of RE
Posted: Mon Feb 11, 2008 4:24 am
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
)
)
Re: Match RE till next occurrence of RE
Posted: Mon Feb 11, 2008 5:37 am
by anjanesh
Ah..thanks - this is new (?=).
How come I missed this pattern syntax in the doc !
Thanks