5 ms·
A couple things: 1. Be careful what you use regex's for. Email addresses are very difficult[0]. HTML is impossible[1]. 2. There are a number of tools that mak
by tghw 13y ago
A couple things:
1. Be careful what you use regex's for. Email addresses are very difficult[0]. HTML is impossible[1].
2. There are a number of tools that make it easier to understand, including [2].
[0] http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html
[1] http://stackoverflow.com/a/1732454/2363 http://stackoverflow.com/a/1732454/2363
[2] http://ivanzuzak.info/noam/webapps/fsm_simulator/ http://ivanzuzak.info/noam/webapps/fsm_simulator/
- rallison 13y ago[0] Yep. Although, to be fair, that is simply a generated expression based on what I assume are much simpler starting rules. That said, and this is a topic that never ceases to come up, be simple and be lenient. For every person who thinks their use case is special, it probably isn't. It is almost always better to do trivial validation that allows false positives than to try to be exact and forget a corner case (and, most likely, you'll end up forgetting multiple corner cases). Want to help users with typos in email addresses without rejecting them? Use something like mailcheck: https://github.com/Kicksend/mailcheck https://github.com/Kicksend/mailcheck And, tons more discussion here: http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address http://stackoverflow.com/questions/201323/using-a-regular-ex... [1] Yep. That said, it is apparently useful for certain parts of HTML parsing/validation, as AntiSamy I believe uses regular expressions frequently. https://www.owasp.org/index.php/Category:OWASP_AntiSamy_Project https://www.owasp.org/index.php/Category:OWASP_AntiSamy_Proj...
- thedufer 13y ago> HTML is impossible. This comes up a lot. Most languages' "regular expressions" aren't, in fact, regular. A true regular expression wouldn't be able to match HTML, but Perl regular expressions (the de facto standard) can because of backreferences. Edit: I'm not saying this is a good idea; it most certainly isn't. I'm just saying its possible.
- ygra 13y agoPerl's regex support can match HTML because of recursive matching, not only because of backreferences (the latter of which is widely implemented, the former not quite so). That being said, there surely are some fun languages that can be matched by what's commonly called regular expressions. Notepad++ was notable (before switching to PCRE) that its "regular expressions" could not even match every regular (or even finite) language (http://stackoverflow.com/a/4815422/73070 http://stackoverflow.com/a/4815422/73070). Many regex engines allow matching languages that are context-sensitive, while at the same time not accepting all context-free languages.