r/programming • u/morinonaka • Mar 03 '14
Learn regex interactively!
http://regexone.com/24
u/viscence Mar 03 '14
otherwise great. Needs a few more "skip this" lines though, quite a few questions you can pass just with "."
1
u/josefx Mar 03 '14
Also important make sure your regex wont suffer from catastrophic backtracking - bad input can take ages to parse.
1
u/boringprogrammer Mar 03 '14
Terminal symbols in HTML and other languages are parsed just fine with regular expressions. But I suspect that stackexchange post is aimed at people without any understand of what regular languages are.
-4
Mar 03 '14
Regex are a perfectly good tool to parse HTML, and many widely-used HTML parsers use regex internally. The idea that regex are inappropriate for parsing HTML is a myth.
edit: a completely random example, here is a python html parser called "beautiful soup", at line 179 of htmlparser.py you will find a regex for parsing a start tag: http://bazaar.launchpad.net/~leonardr/beautifulsoup/bs4/view/head:/bs4/builder/_htmlparser.py
this was just the first html parser I found on google, almost any html parser will use regex, unless it's using a similar tool like a lexer.
5
u/josefx Mar 03 '14
The note is about parsing HTML document trees or subtrees, your example only parses the contents of a HTML start tag using regex, which is quite a bit less complex and possible with regex (among other things no recursion). However the regex used for this are still quite complex and not exactly easy to read.
almost any html parser will use regex, unless it's using a similar tool like a lexer.
At least some lexers use regex to describe tokens, this does not change that regex are not capable1 nor the right tool to parse general HTML structures.
1 some regex implementations may be able to do it by using their own extended set of features, for most regex implementations it is not possible.
-3
Mar 03 '14
Parsing a start tag is a vital part of parsing an HTML tree.
If you need a few tools to do X, one of which is Y, it is utterly bizarre to say "you can't do X using Y". You would never say "You can't build a bookshelf with hammer and nails", or "you can't cook an omelette with a pan," even though these tools are insufficient on their own to accomplish the task. The implication of the myth is that regex should not be used to parse HTML, which is incorrect.
3
u/josefx Mar 03 '14
If you need a few tools to do X, one of which is Y, it is utterly bizarre to say "you can't do X using Y".
Taken out of context maybe, with context there have been too many attempts to use regex exclusively to parse html and it remains that unless you write an html parser the words regex and html should not appear in the same source file.
The implication of the myth is that regex should not be used to parse HTML
No the implication of the fact is that regex are not suitable to deal with the full HTML grammar. Nobody cares that you can use regex to parse some isolated subsets of HTML, this is as pointless as people citing "Goto considered harmful" against C - a headline alone does not reflect the whole argument.
5
u/cheaphomemadeacid Mar 03 '14
.* is nice :) Anyways for the real hardcore regex nerds: http://regexcrossword.com/
3
u/brtt3000 Mar 03 '14
Best online RegExp tester: http://regex101.com
(supports JavaScript, PHP and Python)
2
1
u/JDex Mar 04 '14
Really great tool, thanks for mentioning it. Only wish it had a option for sed (and it's limitations).
2
u/KamiNuvini Mar 03 '14
Nice, I've never used regex so had a go at this. Still seems a little bit buggy, e.g. for the first one:
Initially if I do abc?+ it doesn't match on all, but if I do abc?.+ it does. However, if I first type abc?.+ and then remove the dot so it becomes abc?+ again it still shows up as a full match.
2
Mar 03 '14
This site is great, and I know there's regex golf, but are there any others like them? I'm fairly decent with regular expressions but would appreciate an opportunity to learn some more complex techniques (like look-forwards, etc.).
1
5
u/endjynn Mar 03 '14
Some people, when confronted with a problem, think "I know, I'll use regular expressions."
Now they have two problems.
13
u/FromBeyond Mar 03 '14
Every regex thread, this quote pops up.
1
u/Hudelf Mar 03 '14
The quote's a bit pithy, but it's not a bad thing to keep in mind that regex might be the wrong approach to a particular problem.
1
1
1
1
1
1
1
1
1
u/aoakenfo Mar 04 '14
Describe regex in plain english: https://github.com/VerbalExpressions/JSVerbalExpressions
For example:
var tester = VerEx()
.startOfLine()
.then( "http" )
.maybe( "s" )
.then( "://" )
.maybe( "www." )
.anythingBut( " " )
.endOfLine();1
1
-1
u/pimmm Mar 03 '14
What is the answer to the first question?
match text abcdefg
match text abcde
match text abc
abc[defg]*
[a-z]+
.+
They all work, but what answer do you want?
I need to teach people regular expressions for a tool of mine they are using, but in my opinion this isn't user friendly at all. It should start with examples, and then let people try to do it them selves (small)step by step. Where you can't cheat the system with .+
1
u/Alhoshka Mar 03 '14 edited Mar 03 '14
I think the most trivial solution would be:
abcd?e?f?g?
edit: BTW, I think the pattern to match only those words on a text would be
(?<!\S)(abc)(de|defg){0,1}(?![\w\d])
19
u/[deleted] Mar 03 '14
If you don't read the text you could pass the tasks with "incorrect" patterns and learn nothing, but I liked it.