How "junior" developers can become regex wizards
joshuakemp.blogspot.com
joshuakemp.blogspot.com
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
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
And, tons more discussion here: 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_Proj...
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.
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). Many regex engines allow matching languages that are context-sensitive, while at the same time not accepting all context-free languages.
That said, anything involving extended/perl regex I wind up googling.
1. Write a bunch of regular expressions.
2. Fix them when they break.
Anybody can do this, however junior they may be. (And yes, it does grant you a superpower.)
Tools such as this are shortcuts for newcomers to grok the various operators rapidly. I think they are very worthwhile.
Don't change your existing regular expressions without tests, or Bad Stuff happens.
Regex is used in so many applications and commands, it would be silly not to learn it.
You don't need to be a wizard, but do understand the basics and it will get you far.
Bonus: it makes them easier to diff, too!
We don't write our code on one line with no comments, writing regexps should be no different.
[0] Python example: http://docs.python.org/2/library/re.html#re.VERBOSE
That said, regex is sometimes necessary, so it is important for developers to be competent in this realm. In my opinion, ideally, junior developers would start with using them in non-production environments to become familiar, then go from there. It is also important to be able to distinguish which problems should be solved by regular expressions, and which shouldn't. A good mentor here can be great.
Debugging it was the really fun part but my earlier career with IBM had taught me how to test software effectively and some embedded work with PAL's (an early form of programmable logic) taught me how to really use state machines. I was surprised how little time it took to write and debug. I don't think you can really appreciate the elegant logic of the regular expression language without implementing it.
The best way to learn to love regular expressions is to use them outside of a programming context, where you can get real-time feedback with actual test data. Some text editors will even highlight matches as you type the expression out.
Perhaps for a self-learner a nice approach is to write it yourself and then go searching for a solution. That way you'll likely: validate what you did, figure out that you missed a detail or perhaps learn some new regexp foo or different way of writing the same thing.
This one in particular reminds me of a tool I've always found useful. It's an interactive Regex builder just like the one linked in the OP. I would say it's got some additional compelling features: like mouse-over breakdowns of each expression as you build it, a handy reference list as well, but also a community concept and saved expressions. Really, I've never found anything better. My only complaint is that it's flash-based, but it's an amazing tool, so can't really complain too much.
Any serious developer these days will be using standardised libraries for this sort of validation and not reinventing the wheel with some half baked do-it-yourself regex.
do they really need to be good at regex? we don't all work on the internet you know... regex is basically pointless for most application development. most programmers i consider to be exceptionally talented can not write a regex without reference (although they will do it when necessary by using reference - and very well too).
on the other hand the general approach to problem solving advocated here is quite sound. "find good tools" "don't rush pointlessly" "measure don't guess" "google is good, copy-paste blindly is bad"
And the author didn't say that you must know regex to be a good dev. He said that he was learning it, and that he was putting more effort into it and finding it was paying off.
"I want to be a good developer one day, and I think as a young developer we should put in the extra time to try and really understand something and not just always do what is the quickest."
--edit
I am not a developer.
i've thought of a case i used a regex the last time. and it was removing whitespace in some code file.
Regexes should be seen more like a last resort than a good software engineering choice. They're a code smell. I have seen so many incorrect, slow regexes from people who don't know what they are doing that I have to recommend as a best practice that you don't use them unless you are going to study automata, read Friedl's book, the Dragon book, and study with Tibetan regex monks for years, and mentor everyone who has to maintain your code until you die.