4 ms·
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
by asperous 13y ago
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.
- dyml 13y agoThanks 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.
- mkching 13y agoIf 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.
- humbledrone 13y agoIt 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.
- danieldk 13y agoJust wondering: why do you build a regex and not directly a finite state automaton? Sure, you can not have backreferences, but you can use Hopcroft minimization, etc.
- simpleigh 13y agoI 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+)
- asperous 13y agoYou are right it should be ^ [a-z] [a-z0-9]* @ [a-z]+ \. [a-z]+ $ like someone else pointed out.