Live data from Hacker News

I don't know Regex

ideasof.andersaberg.com

31–40 of 55 posts

Re: I don't know Regex

#31
post #18

Nice work. Personally I will still use the raw regex rather than the method calls to build the regular expression. As another commenter pointed out, the example regex is complex than it should be. It can be reduced to: pat = re.compile(r'^ \w+ @ [A-Za-z]\w* \. \w+ $', re.X) if pat.match('r@acnt.me'): print "woot" I won't bother explaining this regex(too simple). However, if it were something complex, I would put inli…

Seems to me the example regex is the (completely broken) output of the (completely broken) generator expression below.

Oh… that would explain a lot. It does have that "generated by a tool" useless verbosity feel to it.

Re: I don't know Regex

#33
post #18

Nice work. Personally I will still use the raw regex rather than the method calls to build the regular expression. As another commenter pointed out, the example regex is complex than it should be. It can be reduced to: pat = re.compile(r'^ \w+ @ [A-Za-z]\w* \. \w+ $', re.X) if pat.match('r@acnt.me'): print "woot" I won't bother explaining this regex(too simple). However, if it were something complex, I would put inli…

Unless I'm being daft, it can't be reduced to that at all. Dropping matching brackets, removing redundant {1,1} blocks, and escaping the \. we get down to:

    ^[A-Za-z]([A-Za-z]+|\d+)@[A-Za-z]+\.[A-Za-z]+$
Your version isn't the same at all - \w allows letters digits and underscores anywhere, whereas the original is more subtle.

Re: I don't know Regex

#34
post #4

His example could be simplified to ^ ( [a-z0-9]+ @ [a-z]+ \. [a-z]+ ) $ With ignore case and ignore whitespace mode on. I work with Regex a lot so I find this very readable, set in a universal format, and more concise. I will gladly concede that the builder would be easier for those that aren't familiar with regex.

I don't think it can - the local-part of the original matches as (a letter followed by letters) or (a letter followed by numbers):

    [A-Za-z]([A-Za-z]+|(?:\d+))
    =>
    [A-Za-z]([A-Za-z]+|\d+)
Your version doesn't match this: - it allows numbers and digits to be interleaved - it allows the local-part to start with a digit

    [a-z0-9]+ != ([a-z]+|\d+)

Re: I don't know Regex

#35

Your email regex is wrong. There are some obscure email address that will not work. For example my.email domain+plus@some.weird3.com For more see http://en.wikipedia.org/wiki/Email_address#Valid_email_addre...

apparently this is the correct fully rfc-compliant email validation regex: http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html

I might be wrong, but ISTR that no longer works 'cause RFC822 has been superseded (the new RFC extends the domain of valid addresses).

The term "works" is relative of course - I don't suppose anyone actually uses that regex!

Re: I don't know Regex

#36
post #18

Nice work. Personally I will still use the raw regex rather than the method calls to build the regular expression. As another commenter pointed out, the example regex is complex than it should be. It can be reduced to: pat = re.compile(r'^ \w+ @ [A-Za-z]\w* \. \w+ $', re.X) if pat.match('r@acnt.me'): print "woot" I won't bother explaining this regex(too simple). However, if it were something complex, I would put inli…

Unless I'm being daft, it can't be reduced to that at all. Dropping matching brackets, removing redundant {1,1} blocks, and escaping the \. we get down to: ^[A-Za-z]([A-Za-z]+|\d+)@[A-Za-z]+\.[A-Za-z]+$ Your version isn't the same at all - \w allows letters digits and underscores anywhere, whereas the original is more subtle.

... and ignoring case obviously gets us down here:

    ^[a-z]([a-z]+|\d+)@[a-z]+\.[a-z]+$

Re: I don't know Regex

#37
post #16
post #10

I've been making good use of http://www.regexper.com/ since it was linked here. It's made learning regexes much easier as it gives a clear workflow diagram. For example, it showed that the horrible email regex in this article had a couple of errors - the dot before the TLD should be escaped (without the escape, it's 'any character'), and that group #1 can either be letters or digits, but not both (when it can be). It…

Personally I use http://www.debuggex.com/ since it offers a step by step visualization, a live generation of the diagram, a live syntax checking of the regex, etc.

+1 for debuggex. This is the best tool I've seen and I've tried hundreds.

Re: I don't know Regex

#38

Earlier quoted context omitted.

Unless I'm being daft, it can't be reduced to that at all. Dropping matching brackets, removing redundant {1,1} blocks, and escaping the \. we get down to: ^[A-Za-z]([A-Za-z]+|\d+)@[A-Za-z]+\.[A-Za-z]+$ Your version isn't the same at all - \w allows letters digits and underscores anywhere, whereas the original is more subtle.

... and ignoring case obviously gets us down here: ^[a-z]([a-z]+|\d+)@[a-z]+\.[a-z]+$

Yes. If you are going for equivalent, this would be it:

  pat = re.compile(r'^ [a-z] [a-z0-9]* @ [a-z]+ \. [a-z]+ $', re.I | re.X)
I took a few liberties in crafting the reduction.

Re: I don't know Regex

#39
I would like to point out that I am actually the creator of this idea, and not the author. The author has created a variation in C#, that has some differences.

The original repository is at:

https://github.com/thebinarysearchtree/RegExpBuilder

I came up with this idea 2 years ago. Some differences I see between my idea and this c# implementation are:

Or() is confusing by itself. In mine, you pass in objects or strings, such as:

   var regex = new RegExpBuilder()
     .either(pattern1)
     .or(pattern2);

   var regex = new RegExpBuilder()
     .either("sometime")
     .or("soon")
     .or("never");
Also, all the special characters are escaped properly (\ is not escaped).

There are shortcuts - you don't have to do

   .exactly(1).of("hackernews")
you can just do:

   .then("hackernews");
In terms of differences between this and VerbalExpressions, verbal expressions is very limited. It cannot represent many quantifiers (eg, at least 3 of something), does not have decent ways to group subexpressions, and so on. It can only represent (in a practical way), about 0.000001 % of regular expressions, as opposed to RegExpBuilder.

Re: I don't know Regex

#40
Unfortunately you're going to encounter regex a lot in your programming career and this tool won't always be there to save you, so you are going to need to learn regex one way or the other. You might as well get it over with sooner rather than later.

This tool just hindering your progression and yet another abstraction someone has to to learn if they are going to deal with this code. It would make sense if this was a one-off thing and you'd be saving someone the effort of learning some weird protocol or syntax, but since regex is so common and most programmers have just learned to deal with them, you're actually adding more cognitive load, since now they have to know two things instead of one. Imagine coming across this in someone else's code and discovering the regex didn't work as expected. Now I have to debug the regex and figure out whether it's a bug in the tool, or in my regex, etc…

Post reply on HN