Learn regular expressions in about 55 minutes
qntm.org
qntm.org
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
Read the JS docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guid...
Test your regexes: http://regexpal.com/
Visualize your regexes: http://www.regexper.com/
Try some challenges: 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.
Sadly, the only one that does is perl6.
For example, using C, file-local, pre-processor:
#define MATCH_NAME_RE "[a-zA-Z]+"
#define MATCH_SPACES_RE "\s+"
#define CAPTURE_NUMBER_RE "(\d+\)"
And then:
re = QRegex( MATCH_NAME_RE MATCH_SPACES_RE CAPTURE_NUMBER_RE );
It both documents what each part does, makes it easier to debug and change them later on and makes them grokable. It only breaks down when you're doing very complex matching, but most of the time RE can be broken into smaller pieces. a = re.compile(r"""\d + # the integral part
\. # the decimal point
\d * # some fractional digits""", re.X) 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})/;It goes into more detail about some things that were glossed over in the OP, which may be overwhelming for a newbie but necessary for advancing.
I had a look through the regex tuesday list. It looks like fun for people who know their way around regexs but if you're just learning I'd recommend starting with something simpler.
These look like a more reasonable starting level : http://regexone.com/
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. For JavaScript regexes, http://regexpal.com/ is easy to use.
It's excellent, and together with with regular-expression.info a way to learn regexes that at least worked for me.
"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"
I know them, so of course every good developer should know them. (My proof: I am a good developer, and I know them, therefore good developers should know them!)
(NB: In case it's not obvious this comment is slightly tongue-in-cheek.)
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.
That is your example of overkill? More like, exactly the sort of scenario where you'd want to be able to specify a regexp.
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.
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.
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.
I've got news! There are some characters outside ASCII
Here's one if you really want to test your skills: http://i.imgur.com/qLh2gcK.jpg
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.
(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.
Off topic, but, I don't see why people don't make alternatives to this
> Notice! In order to save your progress you have to login with Facebook.
Let me save by cookie as well, please.
It seems there are no shortcuts and you can't practice cut and paste.
Note to self. Learn the syntax.
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.
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.
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/. What am I doing wrong?
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_... grep '"[^"]*"' test.txt
should be about the same, and not reliant on less-standard features.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# because it shows the group.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guid... and https://www.debuggex.com and lots of patience :)
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.
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.
about 55 minutes, meaning +/- 30 years.
i especially loved the page - loaded SUPER fast, no distractions - GREAT job!
love it!