RE: regex question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxx] 
Sent: Monday, May 16, 2005 3:54 PM
To: php-general@xxxxxxxxxxxxx
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....

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux