
Learn regular expressions in about 55 minutes - melloclello
http://qntm.org/files/re/re.html
======
shittyanalogy
You will learn _about_ regular expressions in 55 minutes. To learn _how to
use_ regular expressions, in as long as it might take you, it might be better
to:

Read the wikipedia page which has a great description of what regular
expressions are and why you would use them:
[https://en.wikipedia.org/wiki/Regular_expression](https://en.wikipedia.org/wiki/Regular_expression)

Read the JS docs: [https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Guid...](https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Guide/Regular_Expressions?redirectlocale=en-
US&redirectslug=JavaScript%2FGuide%2FRegular_Expressions)

Test your regexes: [http://regexpal.com/](http://regexpal.com/)

Visualize your regexes: [http://www.regexper.com/](http://www.regexper.com/)

Try some challenges: [http://callumacrae.github.io/regex-
tuesday/](http://callumacrae.github.io/regex-tuesday/)

And remember, writing regular expressions can be very difficult, reasoning
about your regular expressions can be even more so, defining your problem can
be the most difficult of all. Think before you regex.

~~~
zecho
The most difficult part of regex isn't defining the problem. I'd say that
one's easy. The hardest part is figuring out your regex months or years after
you've written it!

~~~
riffraff
extended regexes with comments (aka /x) should be the default and enforced
behaviour in all newly designed languages.

Sadly, the only one that does is perl6.

~~~
deckiedan
Python has them:

    
    
        a = re.compile(r"""\d +  # the integral part
                           \.    # the decimal point
                           \d *  # some fractional digits""", re.X)

~~~
zyxley
Coffeescript has similar functionality. From the Coffeescript docs:

    
    
        OPERATOR = /// ^ (
          ?: [-=]>             # function
           | [-+*/%<>&|^!?=]=  # compound assign / compare
           | >>>=?             # zero-fill right shift
           | ([-+:])\1         # doubles
           | ([&|<>])\2=?      # logic / shift
           | \?\.              # soak access
           | \.{2,3}           # range or splat
        ) ///
    

When turned into Javascript it becomes:

    
    
        var OPERATOR;
        
        OPERATOR = /^(?:[-=]>|[-+*\/%<>&|^!?=]=|>>>=?|([-+:])\1|([&|<>])\2=?|\?\.|\.{2,3})/;

------
roryokane
The tutorial I learned regular expressions from, which is longer but more
detailed than this one, was [http://www.regular-
expressions.info/](http://www.regular-expressions.info/). It’s free, thorough
and well-organized. Its only flaw is that its section on support in various
languages is out of date. It was written to sell a Windows-only regex tool,
but it’s very non-pushy with the advertisements.

You can see a list of online regular expression testers for various languages
at the Stack Overflow “regex” tag wiki, in the “Online sandboxes” section:
[http://stackoverflow.com/tags/regex/info](http://stackoverflow.com/tags/regex/info).
For JavaScript regexes, [http://regexpal.com/](http://regexpal.com/) is easy
to use.

~~~
shirkey
Another excellent resource, which I believe was highlighted a few years ago
here on HN: [http://regexone.com/](http://regexone.com/)

------
adamconroy
Alternative titles :

"How to make your code unreadable in 55 minutes"

"How to make your code hard to debug in 55 minutes"

"How to introduce bugs by copying a regex that you don't understand and
neither did the author"

~~~
SamReidHughes
That's a stupid reply. Regular expressions are very useful.

~~~
mattmanser
Regular expressions are useful, but on a much smaller set of problems than
people typically use them for.

For example Chrome's script skipper in the debug tools completely
unnecessarily uses regexes rather than a simple list of 'contains'.

It's definitely one of the worst "everything's a nail" hammers a lot of
programmers wield. And quite often they've been holding it upside down for
years without even realizing it.

~~~
psquid
Another situation where regexes are possibly abused - syntax highlighting.
Yes, there are a lot of languages where you can sanely highlight /most/ code
with regexes, but there's a non-zero number where you need a little more
context, such as that provided by an AST (which would also help with
autocompletion anyway if the highlighting is being done in the context of an
editor).

------
baby
For the author: I've learned regex for a while, for years, I always come back
to them and I'm always annoyed to forget about them if I don't use them for a
long time.

I use mostly cheat sheets now,

but still, your tutorial was the best I ever read and I actually read it. It
reads better than a "learnXinYminutes.com" and I actually learned some new
stuff.

Notes:

validating email adresses: most MVC have a function that does that. I know
that PHP has that natively as well.

------
JoshTriplett
Handy. This does gloss over some of the notable differences between
implementations (not everything has non-greedy matches or identical {m,n} or
{m,} syntax), but it's still by far the best tutorial introduction I've seen
for regular expressions.

~~~
sdrothrock
Would you mind linking the best tutorial introduction that you've seen for
regular expressions?

I found the parent link incredibly useful because it listed common, useful
expressions and examples of what you do (or don't) get when you use them.

I don't think of it as a tutorial at all since it wasn't really -- more of a
reference or a quick guide.

~~~
JoshTriplett
In my comment above, I said _this_ is the best tutorial introduction I've
seen, even with its few limitations regarding differences between regex
implementations.

~~~
sdrothrock
Whoops! Sorry for misreading that.

I completely misread "Handy" as "Hardly" and then saw "Hardly. This does gloss
over some of the notable differences . . ." which changed the entire tone of
your comment for me.

My bad.

------
lelf
> _\w means the same as [0-9A-Za-z_]_

I've got news! There are some characters outside ASCII

~~~
datr
To be fair, this depends on the implementation. Java, JavaScript and PCRE will
only match ascii characters.

------
AlexSolution
I found [http://regexcrossword.com/](http://regexcrossword.com/) to be a good
learning tool.

Here's one if you really want to test your skills:
[http://i.imgur.com/qLh2gcK.jpg](http://i.imgur.com/qLh2gcK.jpg)

~~~
leeoniya
i'm not sure if this puzzle is meant to be a joke, but it pretty much looks
like it.

things like (O|RM|HHM)* which means match anything. who the hell would every
write (.)(.)(.) when even capturing those is pointless in a crossword.

meh, i say.

~~~
markild
I'm not entirely sure what you are saying, but I actually find this puzzle
quite interesting.

(O|RM|HHM)* doesn't match "anything", but rather "nothing" or something quite
specific. [0]

There are multiple rows in the puzzle where things _can_ match anything, but
they are pretty fast limited by the opposing row/column.

[0]:
[http://www.regexper.com/#%28O|RM|HHM%29*](http://www.regexper.com/#%28O|RM|HHM%29*)

~~~
leeoniya
yeah sorry, wrote that in a hurry. i meant, if it can match nothing and still
be valid, what's the point? you may as well write `.*`

------
gdubs
The time I finally _really_ learned regex was the time I _really_ needed them
for a project that was beyond something I could stack overflow. I think having
some problem in front of oneself, and testing over a ton of input data is a
great way to learn.

------
sjclemmy
I find regular expressions intensely annoying. I come across situations where
they are a perfect fit, but, as I've never got around to learning the syntax I
spend ages looking for a perfect, or sometimes not so perfect, answer on SO
that gives me the arrangement. More often than not, in order to tweak the
answer to make it fit I find myself in the position of having to learn the
syntax in order to do so. Catch 22.

It seems there are no shortcuts and you can't practice cut and paste.

Note to self. Learn the syntax.

------
hvass
I had to learn RegEx because of Google Analytics. The folks from LunaMetrics
wrote a handy guide - [http://www.lunametrics.com/regex-book/Regular-
Expressions-Go...](http://www.lunametrics.com/regex-book/Regular-Expressions-
Google-Analytics.pdf)

It's still just basic RegEx, but if anyone is looking for stuff with more
context related to GA, this is the one for you.

------
leeny
This is awesome. It would be even awesomer if it came with some practice
problems after each section!

~~~
keithpeter
Yes, you are right. Learning needs interaction with the material.

Any experts here want to set an exercise for each section? We can argue about
the _best_ solution in true HN style, and the result will be a good resource.

------
drallison
To really know and understand regular expressions, write a regular expression
pattern matcher.

------
shultays
I have problems with linux and escape characters.

grep '".*\?"' test.txt

"test1" test2 "test3"

matches entire string while article says that ? should force it to match as
little as possible. (All "test1", test2 and "test3" are highlighted as red) It
works correctly [http://regexpal.com/](http://regexpal.com/). What am I doing
wrong?

~~~
clarry
This is when you learn that not all implementations are the same, and realize
it's better to read the documentation for your particular implementation.

I'm guessing your grep defaults to POSIX EREs, but always check the manual.

    
    
      The behavior of multiple adjacent duplication symbols ( '+', '*', '?', and intervals) produces undefined results.
    

[http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_...](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html)

------
gorhill
I don't particularly need a regex course, but I have to say, this site is so
nicely designed, so easy for the eye.

A good way to practice regex is to use one of the many online regex tools to
validate your understanding. My current favorite is
[http://www.gethifi.com/tools/regex#](http://www.gethifi.com/tools/regex#)
because it shows the group.

~~~
Spittie
That's how I "learned" regex.

[https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Guid...](https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Guide/Regular_Expressions#Writing_a_Regular_Expression_Pattern)
and [https://www.debuggex.com](https://www.debuggex.com) and lots of patience
:)

------
drakaal
RegEx 15 minutes to learn, a lifetime to master.

~~~
leobelle
Where mastering hopefully means to just avoid them whenever possible.

------
nickstinemates
Why is it that I find it much easier to just build a lexer/parser than I do
using regex? Something about my brain, every time I see them I usually do
everything in my power to not have to try to understand them.

On the other hand, I've worked with some regex ninjas who appear to just
innately get it.

Maybe it's like cilantro, separating us by genetics.

~~~
omegote
Well I think you're in the "good" side of the discussion. Sometimes whenever
people learn regular expressions, they try to solve everything with them, but
for any kind or medium sized problem or DSLs, writing a custom parser is a
better option.

------
b2themax
I love the summary at the bottom. Concise and to the point... He could have
wrote how to learn regex in 55 seconds.

------
mgkimsal
I do some online training - I'll refer people over to this for a good
refresher/intro to regex. Thanks.

------
NAFV_P
A regex monster:

[http://blog.codinghorror.com/regex-use-vs-regex-
abuse/](http://blog.codinghorror.com/regex-use-vs-regex-abuse/)

------
grey413
That was really concise and well written. I particularly appreciated the clean
warnings and subtle grips about the confusing pats of the syntax.

------
alxndr
Might be nice to include the %r{} style in the "Excessive backslash syndrome"
section.

------
Thiz
The most important things to learn about regex are greed and boundaries.

Nobody cares about 'cat' for that you use a basic replace function in your
preferred language, so when you learn about masks and patterns you need to
understand when to start and when to stop.

Finding everything between quotes ".*" is not gonna give you what you expect.

------
jmnicolas
I'm still waiting for "Reg-ex considered harmful".

~~~
phpnode
no need to wait!
[http://regex.info/blog/2006-09-15/247](http://regex.info/blog/2006-09-15/247)

~~~
jmnicolas
We will need more than a funny sig before the regex madness is ended ! ;-)

------
thomasfoster96
"...in about 55 minutes"

about 55 minutes, meaning +/\- 30 years.

------
srinivasanv
Wish I had this resource when I had to learn regex.

------
erbo
"...and now you have two problems."

------
pan69
This is a very nice overview, but, knowing the rules of chess doesn't make you
a chess master.

~~~
TophWells
Agreed, but 55 minutes isn't going to make anyone a master of anything. The
title is honest.

------
rusmanarief
I love this!

------
flaxin
i've seen many regx sites, this one is DEFINITELY my favorite

i especially loved the page - loaded SUPER fast, no distractions - GREAT job!

love it!

------
binarysolo
Commenting to also save some of the other recs HNers have provided.

~~~
dfc
This is not reddit so you get downvoted for comments like this because you are
not contributing anything to the discussion. In the future you can upvote the
story and then click on your profile and there is a link to saved submissions.
Any submission you upvote will be saved in the saved submission list.
Furthermore on reddit you can just click the "save" link to save things. I
never understood why it was acceptable to post comments like this at reddit
but that is just one of many why does reddit put up with X questions.

~~~
binarysolo
Am not a reddit user, nor did I know this -- but thanks (now I know).

