var letter = "[a-zA-Z]";
var letters = letter + "+";
...
then your regexp would look like this: var regEx = letter ++
letters ++
"|" ++ ...21–30 of 55 posts
var letter = "[a-zA-Z]";
var letters = letter + "+";
...
then your regexp would look like this: var regEx = letter ++
letters ++
"|" ++ ...I also tested my understanding afterwards with some online exercises chosen from these lists:
http://www.emacs.uniyar.ac.ru/doc/em24h/emacs081.htm
http://blogs.msdn.com/ericgu/archive/category/11323.aspx
This reference was handy while doing the exercises: http://www.regular-expressions.info/reference.html
Knowing regexes has been very helpful to me in general. I have used regexes in reformatting my code through find and replace, in finding the code that I need to edit next or that could be causing a certain problem, in writing Apache config URL rewriting rules, in writing poor man’s language parsers that assisted me in generating code, in converting raw data into programming language literals, in understanding user input validation rules, and in other ways. I think that any serious developer who expects to work with more than one programming language in their lifetime should understand regular expressions. Thus, I encourage the OP to try learning regexes, using the resources linked above.
That said, I agree that regexes could be easier to understand. I rather wish that Perl 6’s revised, simpler regex syntax (http://perlcabal.org/syn/S05.html) were the universal standard.
If you use regexes a lot, and get mentally strained by the complexity of some of your bigger ones, consider learning about parsers too, another type of tool that lets you manipulate text in more powerful ways, with longer but more readable code than regexes. http://kschiess.github.io/parslet/ is a simple parsing library to start with if you use Ruby. In fact, Parslet is rather like a more powerful and more theoretically-sound version of the OP’s library RegExpBuilder. Like RegExpBuilder, Parslet uses chains of methods with English names to build parsers.
An example: What are .Letters()? [a-zA-Z]? Are diacritics included? The whole UTF-8 letter range?
And suddenly, you have to specify that character soup and the example goes to hell, because it reintroduces most of complexities in the original regexp.
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.
Tools like sed and regexes are compact and very powerful, and they aren't difficult to learn. I really don't understand the need for this library, which seem needlessly verbose. And you will still need to be able to read regexes in other people's code.
It's a nice idea, and good work, but in my opinion it's solving a problem that doesn't exist.
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.
I learned regexes entirely from the tutorial at http://www.regular-expressions.info/tutorial.html . It clearly explained how the regex engine works so I can simulate it in my head and understand why a given regex does or doesn’t work. I tried out various regexes in TextMate as I read through the tutorial – nowadays I would use one of the online sandboxes listed on http://stackoverflow.com/tags/regex/info . That free…
#!/usr/bin/env ruby
original_regex = /^ ( [a-z0-9]+ @ [a-z]+ \. [a-z]+ ) $/ix
require 'parslet'
include Parslet
local_part = match['A-Za-z0-9'].repeat(1)
letters = match['A-Za-z'].repeat(1)
domain_part = letters >> str('.') >> letters
email_parser = local_part >> str('@') >> domain_part
user_input = "foo#bar.com"
matches_regex = original_regex.match(user_input)
matches_parser = email_parser.parse(user_input)
asperous’s regex: https://news.ycombinator.com/item?id=6319435Parslet info: http://kschiess.github.io/parslet/
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…
I learned regexes entirely from the tutorial at http://www.regular-expressions.info/tutorial.html . It clearly explained how the regex engine works so I can simulate it in my head and understand why a given regex does or doesn’t work. I tried out various regexes in TextMate as I read through the tutorial – nowadays I would use one of the online sandboxes listed on http://stackoverflow.com/tags/regex/info . That free…
Here is one possible translation of asperous’s simplified email regex into a Parslet parser: #!/usr/bin/env ruby original_regex = /^ ( [a-z0-9]+ @ [a-z]+ \. [a-z]+ ) $/ix require 'parslet' include Parslet local_part = match['A-Za-z0-9'].repeat(1) letters = match['A-Za-z'].repeat(1) domain_part = letters >> str('.') >> letters email_parser = local_part >> str('@') >> domain_part user_input = "foo#bar.com" matches_rege…