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.
I don't know Regex
31–40 of 55 posts
Re: I don't know Regex
#32Re: I don't know Regex
#33Nice 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…
^[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
#34His 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.
[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
#35Your 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
The term "works" is relative of course - I don't suppose anyone actually uses that regex!
Re: I don't know Regex
#36Nice 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.
^[a-z]([a-z]+|\d+)@[a-z]+\.[a-z]+$Re: I don't know Regex
#37I'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
#38Earlier 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]+$
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
#39The 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
#40This 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…