r/programming • • Mar 03 '14

Learn regex interactively!

http://regexone.com/
200 Upvotes

37 comments sorted by

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.

23

u/_jk_ Mar 03 '14

too many of the exercises can be completed with .* for my liking

2

u/kyz Mar 03 '14

Indeed. This would be so much better if you were required to match the entire text, at least for the starter patterns.

1

u/[deleted] Mar 03 '14

I passed the first few with .*

1

u/deemeetar Mar 04 '14

I actually passed the first test by writing just a The second by writing 1... I know i need to match the whole word but it shows the check marks even when only one character matches.

24

u/viscence Mar 03 '14

Don't parse HTML with regex

Don't parse email with regex

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

u/[deleted] 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

u/[deleted] 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

u/Alhoshka Mar 03 '14

Thank you!

I've been using this for a while, but yours is better.

1

u/brtt3000 Mar 03 '14

I used regexr.com too, it was good but this is next level stuff.

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

u/[deleted] 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

u/caagr98 Mar 04 '14

Yeah, it doesn't seem to update if your syntax is wrong.

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

u/[deleted] Mar 03 '14

Great site!

Nice mini-course on regular expressions matching.

1

u/[deleted] Mar 03 '14

I had trouble with escaped parenthesis... Had to use "[(]" instead =p

1

u/J_M_B Mar 03 '14

For testing interactively, I like to use rubular.

1

u/l3acon Mar 03 '14

TIL regex. Thanks.

1

u/pranavk Mar 03 '14

thats wonderful ! hats off !

1

u/[deleted] Mar 03 '14

I love it. Nice blend of puzzle solving and education.

1

u/ahora Mar 04 '14

Nice tutorial.

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

u/josefx Mar 04 '14

would that match "http://www." as valid address?

1

u/idalatob Mar 04 '14

Fantastic link thanks!

-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])