Live data from Hacker News

I don't know Regex

ideasof.andersaberg.com

11–20 of 55 posts

Re: I don't know Regex

#11

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

Only if you do not add comments.

There was a fun proposal to avoid spam by adding comments inside your email address. Any true emailer should handle it. And regex should break.

Unfortunately Microsoft Exchange breaks. And that case is important enough that the idea never caught on.

Re: I don't know Regex

#12
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…

Great resource, it seems immensly useful! I will be sure to investigate any errors on my part! :)

And as I've said on the Github Repo, the library is not quite ready for prime time. When it's stable, i'll be sure to publish a nuget package for simple access.

Re: I don't know Regex

#13

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...

I think that actually says a lot about regexes as code.

The more corner cases you have to consider, the more unreadable the code gets, after a while by seemingly exponential degree.

And if you have a tool to build the regex, why not just use the tool's code as your source so the final result is readable.

Basically, pasting a big regex into your code hardly seems more desirable than pasting a bunch of assembler there. But assembler can unavoidable. This isn't.

Regex are cool as CS constructs though.

Re: I don't know Regex

#14

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

These guys say that a 430-character regex does the same trick as that 6k-character one (down in the RFC 2822 section)

http://www.regular-expressions.info/email.html

Re: I don't know Regex

#15
Seems there is a bug, since this:

    .Exactly(1).Of(".")
expands to this:

    (.{1,1})
Which is wrong, as dot is a meta-character. It should be escaped.

Re: I don't know Regex

#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.

Re: I don't know Regex

#17
post #15

Seems there is a bug, since this: .Exactly(1).Of(".") expands to this: (.{1,1}) Which is wrong, as dot is a meta-character. It should be escaped.

You are absolutely right, i'll get right on this bug during lunch break (i'm at the office, so..).

Thanks alot for the report!

Re: I don't know Regex

#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 inline comments:

    pat = re.compile(r'''^ \w+  # rahul
                    @
                    [A-Za-z]\w*  # thoughtnirvana
                    \.
                    \w+ # com
                    $''',
                    re.X)
Notes about the example regex:

  var regEx = {(?:^)[A-Za-z]([A-Za-z]+|(?:\d+))(@{1,1})[A-Za-z]+(.{1,1})[A-Za-z]+(?:$)}
(?:^), (?:$) - This is the same as simply using ^. It isn't captured by default so there isn't a need to mark it non-capturing.

([A-Za-z]+|(?:\d+)) - What's going on here? You have a capturing group and within that capturing group, you have the or part marked as non capturing. What's the intent?

(@{1,1}) - @{1,1} is the same as @. Also, why are you capturing it? I think you are using parens for making the regex readable. You should use the IgnorePatternWhitespace instead http://msdn.microsoft.com/en-us/library/yd1hzczs.aspx

Re: I don't know Regex

#19
post #9
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.

Thanks for the comments! I bet I could improve the way regex is generated, since i'm not so comfortable working with regex. I'd also like to add features, a .Not operator would be really useful, and I'd gladly take a pull request if anyone have an implementation in mind :) If I receive some signals that others find this library useful and would like me to add some feature, I'd be more than glad to do so.

If I came across the code in the original post, I would be confused as to what the Or operator applied to. With a regexp, the parenthesis make this clear.

I would also assume that Exactly(1).Of(".") was meant to match a literal ".". In a PCRE, you can surround a section with \Q...\E to force literal interpretation, but I believe in .NET you would need to call Regexp.Escape.

The overall concept is not a terrible idea, but you should probably become a little more familiar with regexps before trying to write a library that creates them. While some things in the sample seem a natural product of being code generated (e.g. "@{1,1}" instead of simply "@"), the use of "(?:" in many places is simply not needed.

Re: I don't know Regex

#20
post #9
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.

Thanks for the comments! I bet I could improve the way regex is generated, since i'm not so comfortable working with regex. I'd also like to add features, a .Not operator would be really useful, and I'd gladly take a pull request if anyone have an implementation in mind :) If I receive some signals that others find this library useful and would like me to add some feature, I'd be more than glad to do so.

It seems a little misleading to write "Which one of these snippets would you like to encounter within your source code" and then go on to compare the generated regex code with the hand-written builder code. The generated regex is much uglier, more redundant, and more complex than what a person familiar with the syntax would write (e.g. grandparent's example regex).

Also, does the builder library support captures (i.e. pulling out substrings that match subpatterns)? That is a pretty important feature for a regex library.

Post reply on HN