Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Sun Feb 10, 2008 8:05 am
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
GeertDD
Forum Contributor
Posts: 274 Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium
Post
by GeertDD » Sun Feb 10, 2008 11:11 am
Code: Select all
preg_match_all('#[a-z]+|[A-Z]+#', 'abcdeABCDEFGHIJKabcdefg', $matches);
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Sun Feb 10, 2008 9:19 pm
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
)
)
GeertDD
Forum Contributor
Posts: 274 Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium
Post
by GeertDD » Mon Feb 11, 2008 2:16 am
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
)
)
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Mon Feb 11, 2008 2:24 am
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);
GeertDD
Forum Contributor
Posts: 274 Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium
Post
by GeertDD » Mon Feb 11, 2008 4:24 am
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
)
)
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Mon Feb 11, 2008 5:37 am
Ah..thanks - this is new (?= ).
How come I missed this pattern syntax in the doc !
Thanks