RE: regex question
| From: | Carl Furst | Date: | Tue, 17 May 2005 18:44:47 +0000 |
| Subject: | RE: regex question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-215401@lists.php.net to get a copy of this message | ||
I think there maybe a few ways to do this... One is
[^@]@[^@]
That basically says "find me the pattern where one non-at symbol is followed
by an at symbol followed by another non-at symbol"
So if you do
<?php
$string = "@ one more T@@me for @ and i@ the Bl@@dy n@nsense";
$pattern = '/[^@](@)[^@]/';
$numMatch = preg_match_all($pattern, $string, $matches);
echo "numMatch: $numMatch\n";
print_r ($matches);
?>
numMatch: 3
Array
(
[0] => Array
(
[0] => @
[1] => i@
[2] => n@n
)
[1] => Array
(
[0] => @
[1] => @
[2] => @
)
)
Well, where that fails is when you have @'s at the beginning or end of the
string and that's easy enough to do.. So that would mean three searches...
There's probably a way to integrate them into one without loosing integrity,
but it depends on what kind of regexp libs you have, I reckon. It also
depends on what you really are trying to do with this search. Consider
str_replace, strpos and strtr as well.
Thanks,
Carl
-----Original Message-----
From: Al [mailto:news@ridersite.org]
Sent: Monday, May 16, 2005 3:54 PM
To: php-general@lists.php.net
Subject: regex question
What pattern can I use to match ONLY single occurrences of a character in a
string.
e.g., "Some text @ and some mo@@re and mor@e, etc @@@.
I only want the two occurrences with a single occurrence of "@".
@{1} doesn't work; there are 4 matches.
Thanks....