Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

As someone who has crafted thousands of complex regular expression rules for data capture, here is my take:

1. This is a fine idea to aid regex newbies in crafting their expressions. I see this as a gateway instead of a longterm tool. The expressions won't be optimal (by no fault of the tool), nor will they likely be complete, but that's not the point. If it helps reduce the barrier(s) to adoption of regular expressions, then I can heartily support it.

2. To the people who say they use regular expressions only a handful of times a year, thus it's not worthwhile to invest time in learning the syntax, I offer this: once you know it, you will use it far more often than you ever expected. Find & replace text, piping output, Nginx.conf editing, or even the REGEXP() function in MySQL. It's a valuable skillset in so many environments that I expect you will use weekly, if not daily.

3. Ultimately regular expressions, like everything, are extra difficult until you know all of the available tools in the toolbox. At that point, you may realize you wrote an unnecessarily complex expression simply because you didn't know better.



There is another benefit to using regular expressions for replacements that is not obvious, but it is a huge productivity boost: When I decide to modify code using regexes instead of doing it by hand, my work becomes more transactional, and as a result I miss fewer errors when I realize that I need to re-do my work.

For example, let's say I have a bunch of function calls that need an extra parameter passed at the end. In vim, I might do something like

    114,155s/);/, extra_parameter);/g
Of course, it might have been faster to copy and paste a bunch of times, but then I realize that I actually need 2 parameters, not one. Now I can just press 'u' do undo what I just did, escape colon, then 'up' to get the last regex which I can quickly modify:

    114,155s/);/, extra_parameter, yet_another_parameter);/g
When you attempt to do and undo this stuff by hand the chances become very high that you will make subtle mistakes somewhere. In my experience, when I use regexes it is more likely that everything is right or everything is wrong in an obvious way.


Re #2: As someone who more or less lives in the Linux command line. I concur with this. Once you know regexes well you can find productive time-saving uses for them dozens of times a day. I rarely go a couple of days without using a regex, never mind a couple of times of year.


Can you explain how you edit your nginx configuration files with regex? Just replacing?


Ah for Nginx, specifically, I meant to refer to location blocks. Granted, regex should be used very carefully for performance reasons, but when appropriate you can do some really cool pattern matching.


Oh, that makes more sense. Thanks for sharing :)


but ... wasn't the nginx regexp syntax different to the standard unix one?


Thanks for this. Makes a difference to hear. OP




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: