Live data from Hacker News

Stop avoiding regular expressions damn it

bradt.ca

31–40 of 55 posts

Re: Stop avoiding regular expressions damn it

#31
post #17

Earlier quoted context omitted.

Thanks for clarifying. I guess my point was that if you're just matching one email address in a form submission for example, is performance significant?

No, a few µs vs a few ns when processing your web form won't be significant. Don't shy away from regular expressions, but be aware of their performance and readability impact. The problem is when developers that don't know any better build parsers with regular expressions. That's almost always a bad idea.

Agreed.

Re: Stop avoiding regular expressions damn it

#32
post #24

More concise? Sometimes. Slower? Always. BenchmarkRegexp 500000 5136 ns/op BenchmarkStrings 10000000 173 ns/op http://play.golang.org/p/YT29Ao-tOt

You could more than double the performance of the regexp if you did MustCompile just the once rather than within every loop. MustCompile is generally used to make the regexp a global so that it isn't done over and over. Just move it out of the loop, as it's really not necessary to compile regular expressions every time you want to match/replace against it.

If I am reading the chart correctly, doubled performance would still not be enough.

Re: Stop avoiding regular expressions damn it

#33

More concise? Sometimes. Slower? Always. BenchmarkRegexp 500000 5136 ns/op BenchmarkStrings 10000000 173 ns/op http://play.golang.org/p/YT29Ao-tOt

Last time I checked, any time I needed the power and flexibility of a using a regular expression. Getting the job done was far more and over a degree of magnitude more important than saving some milliseconds of processing time.

Re: Stop avoiding regular expressions damn it

#34

Stop propagating bad interfaces like 'regular expressions' damn it! An interface that e.g. makes me 'escape' half of my input because its designers think their special use of characters must take precedence over all user input is a bad interface.

Many programming languages have a function for that to do that for you...

In Perl, it's called quotemeta (qw, qq and family, too), in Python and Ruby it's .escape... and there's always \Q ... \E to use...

I'm sure others have similar methods/functions.

Re: Stop avoiding regular expressions damn it

#35

Stop propagating bad interfaces like 'regular expressions' damn it! An interface that e.g. makes me 'escape' half of my input because its designers think their special use of characters must take precedence over all user input is a bad interface.

I agree with that criticism, so do other people, that's why the implementation of Regular Expressions are anything but regular.

That always makes me giggle.

Re: Stop avoiding regular expressions damn it

#36
post #32
post #24

Earlier quoted context omitted.

You could more than double the performance of the regexp if you did MustCompile just the once rather than within every loop. MustCompile is generally used to make the regexp a global so that it isn't done over and over. Just move it out of the loop, as it's really not necessary to compile regular expressions every time you want to match/replace against it.

If I am reading the chart correctly, doubled performance would still not be enough.

Absolutely. But the original version linked was twice as slow as need be.

For trivial replacements string manipulation I find is faster and safer (fewer bugs). But there is some threshold of complexity in which regular expressions are both more performant and safer.

Re: Stop avoiding regular expressions damn it

#37

The core criticism of regular expressions is legitimately directed at intermediate programmers who know enough to be dangerous, but is sometimes inappropriately cargo-culted by beginner programmers who use it as an excuse not to learn regular expressions. The fact is that despite pithy slogans, there is a sweet spot where a regular expression does the job of matching a string in a clearer fashion than anything else.…

Though there's nothing wrong with busting out a baroque regex in one time use contexts (editor's search function, throw-away application of grep or sed, &c).

Re: Stop avoiding regular expressions damn it

#38

Stop propagating bad interfaces like 'regular expressions' damn it! An interface that e.g. makes me 'escape' half of my input because its designers think their special use of characters must take precedence over all user input is a bad interface.

So, what do you use instead?

Re: Stop avoiding regular expressions damn it

#39

More concise? Sometimes. Slower? Always. BenchmarkRegexp 500000 5136 ns/op BenchmarkStrings 10000000 173 ns/op http://play.golang.org/p/YT29Ao-tOt

That is an implementation specific benchmark. A grungy real-world regexp engine (such as Perl's) usually will recognize important special cases and substitute in faster code for them.

The classic example is to recognize that you're looking for a fixed string, and substitute in Boyer Moore. But prefix/suffix recognition are two other common examples.

Post reply on HN