Email address capture with exclusion

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
anod
Forum Newbie
Posts: 5
Joined: Wed Nov 14, 2007 12:23 pm
Location: Israel
Contact:

Post 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...
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post 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.
anod
Forum Newbie
Posts: 5
Joined: Wed Nov 14, 2007 12:23 pm
Location: Israel
Contact:

Post 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"
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

$pattern = '/([^(support|contact)]@domain\.com/';
Maybe.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post 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/
anod
Forum Newbie
Posts: 5
Joined: Wed Nov 14, 2007 12:23 pm
Location: Israel
Contact:

Post 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....
anod
Forum Newbie
Posts: 5
Joined: Wed Nov 14, 2007 12:23 pm
Location: Israel
Contact:

Post by anod »

2GeertDD

Thanks.. :lol:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

This thread has been split from another thread. While this topic is similar, they are unique enough to be separated.
Post Reply