News for hackers. Stuff that matters.
Re:A Liberal, Accurate Regex Pattern for Matching URLs by notauser on Friday November 27, 2009 @04:06PM (#964370)
Great stuff, my URL matching regex was very limited.

For e-mails I use:

  /\b[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9]([-a-z0-9_]?[a-z0-9])*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z]{2})|([1]?\d{1,2}|2[0-4]{1}\d{1}|25[0-5]{1})(\.([1]?\d{1,2}|2[0-4]{1}\d{1}|25[0-5]{1})){3})(:[0-9]{1,5})?\b/ig
And for twitter user names I use:

  /\B@\w+\b/ig
(which incorrectly matches @@username as @username but I assume that kind of thing is a typo - the important thing is not to match e-mail addresses)
Re:A Liberal, Accurate Regex Pattern for Matching URLs by silentbicycle on Friday November 27, 2009 @04:31PM (#964393)
An RFC-822-compliant regex is listed at (http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html), but if anything, it's just a strong using for using real parsing tools. Regexes don't handle recursion and balanced delimiters very well.
Re:A Liberal, Accurate Regex Pattern for Matching URLs by jerf on Friday November 27, 2009 @06:08PM (#964498)
For validating emails, I've settled on /.@./, or if you really want to push for valid emails, /.@[^.]+\../. (Note the lack of anchoring to the beginning or end.) (That, and some limit on length.)

The rules are so flipping complicated and so easy to get wrong that you're better off just trying to send a mail and seeing what happens, and asking the recipient to validate reception if you care about the address. Is it really that important to exclude bad emails, at the cost of, say, blocking email addresses from the UK, as your regex seems to do? Even "validating" for sheer user error is only useful if you get it right.

Re:A Liberal, Accurate Regex Pattern for Matching URLs by cschneid on Friday November 27, 2009 @08:19PM (#964647)
I like soft validation for emails. "This doesn't look like an email address, verify you typed everything correctly, and resubmit". That way you handle legit typos, without hassling people who have weird emails (gmail plus signs and such).
Re:A Liberal, Accurate Regex Pattern for Matching URLs by notauser on Saturday November 28, 2009 @05:41AM (#965024)
I'm actually use it to find addresses in text, for validation I usually use in the Django built in which is very liberal.

Incidentally this does correctly pick up UK e-mail addresses (tested with .co.uk .org.uk and .net.uk).

Re:A Liberal, Accurate Regex Pattern for Matching URLs by blasdel on Friday November 27, 2009 @05:11PM (#964439)
Indent your lines with two spaces and they'll end up in a <pre> with your asterisks intact.

Why bother trying to validate the domain lexically, when you can just try resolving it?

Re:A Liberal, Accurate Regex Pattern for Matching URLs by notauser on Friday November 27, 2009 @05:32PM (#964461)
Thanks, fixed the formatting.

(It's not my regex - I lifted it from someone who had the time to write a comprehensive test suite.)