Page 1 of 1

Posted: Wed Nov 14, 2007 1:50 pm
by anod
Can you help me with the same problem?

I need to replace all emails in text that not support@domain.com or contact@domain.com
my code replace all matches:

Code: Select all

$str = "email@domain.com alex@domain.com test@domain.com  support@domain.com some text here @domain.com contact@domain.com";
$rstr = "$1support@domain.com";
$pattern = "/([\s|\,]*)([a-zA-Z0-9\.]*?)(\@domain\.[a-z]{2,6})/";
echo preg_replace($pattern,$rstr,$str);
I tried something like this:

Code: Select all

$pattern = "/([\s|\,]*)([a-zA-Z0-9\.]*?!support|!contact)(\@domain\.[a-z]{2,6})/";
but the pattern didn't work for me...

Posted: Wed Nov 14, 2007 2:37 pm
by GeertDD
I don't know what it is with adding a "!" in front of text... It does not negate the stuff following it, if that's what you were thinking.

A proper negative lookahead looks like (?!tada).

Try again with that in mind.

And by the way, don't put "|" inside character classes (unless you want to match the actual pipe character as well). Also, no need to escape commas or dots inside character classes.

Posted: Wed Nov 14, 2007 3:59 pm
by anod
I don't understand something... i tried

Code: Select all

$pattern = "/([\s|\,]*)([a-zA-Z0-9\_\.\-]*)(?!support)@domain.com/";
or

Code: Select all

$pattern = "/([\s|\,]*)([a-zA-Z0-9\_\.\-]*?!support)@domain.com/";
no results...
Very difficult for me to understand the logic of patterns...
I need something simple like this: "if not 'support' then replace"

Posted: Wed Nov 14, 2007 4:25 pm
by s.dot

Code: Select all

$pattern = '/([^(support|contact)]@domain\.com/';
Maybe.

Posted: Wed Nov 14, 2007 11:29 pm
by GeertDD
This regex matches all @domain.com emails, except for support and contact. Hope that is what you're looking for.

Code: Select all

/\S+(?<!\bsupport|\bcontact)@domain\.com/

Posted: Wed Nov 14, 2007 11:51 pm
by anod
php:
$pattern = '/([^(support|contact)]@domain\.com/';


Maybe.
Thanks... This pattern don't replace first part of email
I did small changes in your patter,
$pattern = "/[a-zA-Z0-9\_\.\-]*[^\s(support|contact)][a-zA-Z0-9\_\.\-]*@domain.com/";
i don't understand why, but now it's working....

Posted: Thu Nov 15, 2007 12:03 am
by anod
2GeertDD

Thanks.. :lol:

Posted: Thu Nov 15, 2007 11:22 am
by feyd
This thread has been split from another thread. While this topic is similar, they are unique enough to be separated.