4 ms·
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
by irahul 13y ago
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 http://msdn.microsoft.com/en-us/library/yd1hzczs.aspx
- masklinn 13y agoSeems to me the example regex is the (completely broken) output of the (completely broken) generator expression below.
- jobigoud 13y agoOh… that would explain a lot. It does have that "generated by a tool" useless verbosity feel to it.
- simpleigh 13y agoUnless 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.