3 ms·
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-z
by notauser 17y ago
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)
- silentbicycle 17y agoAn RFC-822-compliant regex is listed at (http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html 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.
- blasdel 17y agoIndent 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?
- notauser 17y agoThanks, fixed the formatting. (It's not my regex - I lifted it from someone who had the time to write a comprehensive test suite.)
- jerf 17y agoFor 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.
- cschneid 17y agoI 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).
- notauser 17y agoI'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).