4 ms·
I learned regexes entirely from the tutorial at http://www.regular-expressions.info/tutorial.html http://www.regular-expressions.info/tutorial.html. It clearly
by roryokane 13y ago
I learned regexes entirely from the tutorial at http://www.regular-expressions.info/tutorial.html 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 http://stackoverflow.com/tags/regex/info. That free tutorial was enough to get me very comfortable with regexes.
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://www.emacs.uniyar.ac.ru/doc/em24h/emacs081.htm
http://blogs.msdn.com/ericgu/archive/category/11323.aspx http://blogs.msdn.com/ericgu/archive/category/11323.aspx
This reference was handy while doing the exercises: http://www.regular-expressions.info/reference.html 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 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/ 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.
- roryokane 13y agoHere 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_regex = original_regex.match(user_input) matches_parser = email_parser.parse(user_input) asperous’s regex: https://news.ycombinator.com/item?id=6319435 https://news.ycombinator.com/item?id=6319435 Parslet info: http://kschiess.github.io/parslet/ http://kschiess.github.io/parslet/
- deleted 13y ago[deleted]