11 ms·
I really don't understand why some people are so afraid of regex. Sure, it's not perfect, but these symbol-less solutions feel to me like one step forward and t
by cthor 13y ago
I really don't understand why some people are so afraid of regex. Sure, it's not perfect, but these symbol-less solutions feel to me like one step forward and two steps back. A more interesting development would be something along the lines of what Perl 6 is trying to do (if only someone could implement it).[1]
We should start treating regex like a computer language in its own right, rather than some second-hand citizen that we stuff into a single line without any delimiting whitespace. Use the /x switch and comment your code as you would with any other programming language, and you'll find that regex really isn't that scary.
Take the example pattern. The regex written by a human might look something like so:
m{^
https? :// # Protocol
(?: \w+ \. )? # Subdomain
([\w\-]+) \. # Domain
(?: com | org ) # TLD
/?
$}x
It's certainly easier to parse. Debugging it is also a lot easier. We can see that this won't match anything with more than one subdomain. We can also see that it won't match subdomains that have hyphens in them. It also looks at a glance much more like a URL and less like some arbitrary Ruby code.
[1]: http://www.perl6.org/archive/doc/design/apo/A05.html http://www.perl6.org/archive/doc/design/apo/A05.html
- lelf 13y ago> something along the lines of what Perl 6 is trying to do (if only someone could implement it) What, like, perl6? grammar URL { rule TOP { <proto> '://' <domain> '/' } token proto { 'http' 's'? } token subdom { \w+ } rule domain { <.subdom> [ '.' <.domain> ]? } } say URL.parse('http://lelf.lu/'); Betty:hacks lelf$ perl6 gr.p6 「http://lelf.lu/」 proto => 「http」 domain => 「lelf.lu」
- draegtun 13y agoAnd for those interested a full RFC 3986 (URI) in Perl6 grammar can be found here - https://github.com/ihrd/uri https://github.com/ihrd/uri Here is direct link to grammar module - https://github.com/ihrd/uri/blob/master/lib/IETF/RFC_Grammar/URI.pm https://github.com/ihrd/uri/blob/master/lib/IETF/RFC_Grammar...
- jrochkind1 13y agoi have no trouble writing regexes, even quite complicated ones. I probably end up writing a really complicated one once a month or so; but basic ones weekly or daily. I have no trouble writing them. What I have trouble doing, when it comes to very complicated regexes, is _reading_ what I've already written a couple months ago (myself! I pity those who aren't me even more). And even more so, when it comes to complicated regexen, _modifying_ what I've already written to fix a bug or change it slightly for new requirements. A complicated regex meets the requirements for the old joke about 'write-only languages.' Somewhat ameliorated if you use the Perl-originated conventions for insignificant whitespace in a regex, which I _think_ ruby does too now, but I am not sure, cause I never use it, cause I can never remember how it works and never bother to look it up (have you tried looking up docs on ruby regexes on the web? good luck! I have no idea _where_ the official docs, if any English exist, are.) I have no trouble writing regexes. I think every programmer should be comfortable doing so. I think using this tool without understanding regex basics first is a mistake. And I _still_ think this is a kinda neat and potentially useful tool.
- krainboltgreene 13y agoLibrary owner here, just want to point out that m{^ https? :// # Protocol (?: \w+ \. )? # Subdomain ([\w\-]+) \. # Domain (?: com | org ) # TLD /? $}x Isn't really any different from hex {starts protocol subdomain domain tld maybe("?") ends}
- danudey 13y agoI think you mean 'maybe("/")'?
- krainboltgreene 13y agoI did!
- Someone 13y agoI tend to write such code without comments (using some pseudo-language): Protocol = 'https?://' Subdomain = '\w+' Domain = '[-\w]+' TLD = 'com|org' Re = WholeTextMatches( Protocol + Optional( Subdomain + '.') + Capture(Domain) + TLD) Typically, I wouldn't go that far, but storing separate parts into well-named strings before assembling the entire regex, IMO, makes things much more readable.
- thedufer 13y agoCoffeeScript's block regex (delimited by /// instead of /) does exactly this. It is a vast improvement over normal regular expressions, and I even prefer it to the syntax described in this post (which is unreasonably verbose).