Jeff and .NET

The .NET musings of Jeff Putz

Sponsors

News

My Sites

RegEx Challenge III

I admit I'm mentally weak. I can usually get a regular expression to work until I have to come up with something to NOT match. Here's today's challenge. Match only e-mail addresses that are NOT preceded by a colon or closing bracket. Your test string is this:

test[url="mailto:name1@domain.com"]name2@domain.com name3@domain.com test

Only name3@domain.com should return a match. For simplicity's sake, I'm using this for the actual e-mail address matching (I realize it isn't perfect and may allow non-RFC compliant addresses through):

([\w\-\.]*@([\w\-])+(\.[\w\-]+)+)

Here's a handy test page. The prize is grand recognition and a pat on the back. It's the best I can offer... I'm poor. :)
Posted: Nov 23 2004, 02:13 PM by Jeff | with 7 comment(s)
Filed under:

Comments

domovoi said:

\s+[^:\s]*\@[^\s]*
# November 23, 2004 4:45 PM

Drew Marsh said:

(?<=\s)(?<email>(?<user>[\w\-\.]*)@(?<domain>([\w\-])+(\.[\w\-]+)+))

NOTE: You can pull the groups out of there if you're not interested in explicitly capturing them.
# November 23, 2004 7:09 PM

Jeff said:

Both of these seem to work... unless the address is at the start of a line.
# November 23, 2004 9:28 PM

Drew Marsh said:

Sorry, I was just using your sample. No problem on beginning of line though. I actually bached the last one a little anyway, so here goes.

(?<email>(?<=\s|^)(?<user>[\w\.\-_']+)@(?<domain>([\w\-]+\.)+[\w\-]+))

# November 24, 2004 10:48 PM

Jeff said:

OK, I obviously don't give you enough test conditions to work with, but I also need to detect it within <p> tags, i.e., it should find the address in:

<p>test@test.com</p>

I assume that means changing something in that first group?
# November 25, 2004 1:54 PM

Jeff said:

Actually, scratch that... I think my logic is all out of order.
# November 25, 2004 1:58 PM

Jeff said:

Grrrr... I keep discovering that my test coverage isn't good enough to catch what I'm really after. It should still match:

[i]test@test.com[/i]

but not:

[url="mailto:test@test.com"]test@test.com[/url]
# November 26, 2004 1:23 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)