Live data from Hacker News

Is it a must for every programmer to learn regular expressions?

programmers.stackexchange.com

51–60 of 60 posts

Re: Is it a must for every programmer to learn regular expressions?

#51
I think every programmer should know them, but should try to avoid using them unless they really are the best solution to your problem. Too many people reach for them too soon, leaving hard to understand and hard to maintain code that could have been better expressed some other way.

Re: Is it a must for every programmer to learn regular expressions?

#52

Yes, it is, without a doubt. It's one of the most universal tricks of the trade that you'll literally never regret learning, mainly because just about any environment you'll ever work in will by necessity support regexes, and in many, it will be the primary way you interact with text. But it's also a must that you realize that despite the fact that they're exceptionally useful and widely supported, regexes are a disg…

I think that regular expressions have a good syntax for most typical (small) uses of regular expressions. Maybe the choice of special characters isn't ideal, and it might be nice to have 'English' versions of more special characters (similar to e.g. [[:digit:]] in POSIX), but for small regexes the (mostly) one-to-one mapping between characters in the pattern and characters in the string is a very nice and intuitive syntax.

I think the real problem is that we lack (or don't learn) good tools to bridge the gap between regular expressions and 'custom parser'. We're reluctant to refactor from '1 line of just-starting-to-be-horrible regex' to tens or hundreds of lines (depending on language and libraries) to do it 'properly', and so we end up stretching regular expressions beyond the point where they make life easier.

Perl has Parse::RecDescent (and probably several others), which is pretty close to the right thing, and clearly it's very doable in a lot of languages - anyone got any suggestions in other languages?

Re: Is it a must for every programmer to learn regular expressions?

#53
From time to time I re-read Larry Wall's "Apocalypse 5" - http://www.perl.com/pub/2002/06/04/apo5.html?page=6 It is still in interesting look into why regexs are the way they are.

Also if you are familiar with this quote:

  "Some people, when confronted with a problem, think 
  'I know, I'll use regular expressions.'   Now they have two problems."
Read Jeffrey Friedl's research into the quote - http://regex.info/blog/2006-09-15/247

Re: Is it a must for every programmer to learn regular expressions?

#54
post #17

Learn the 20% that you will use 80% of the time. If your not willing to do that your probably not serious about programming in the first place.

Amen to that. For reasons I can only guess at, I'm the "regex guy" at work. When people have a regex question they come to me. And the interesting thing is that the answers are not hard, well within the 20% area. I've found that most people don't care to learn how to use character classes, or even what they're for. For example, to match the single letter 'i', I've seen people write:

    [i]
Many don't know the difference nor implications of .* ? vs .*

And on and on. It amazes me that these people actually code for a living.

Re: Is it a must for every programmer to learn regular expressions?

#55
Probably worth revisiting this bit of commentary from Rob Pike:

"Regular expressions are hard to write, hard to write well, and can be expensive relative to other technologies... Standard lexing and parsing techniques are so easy to write, so general, and so adaptable there's no reason to use regular expressions.

"Another way to look at it is that lexers and parsing are matching statically-defined patterns, but regular expressions' strength is that they provide a way to express patterns dynamically. They're great in text editors and search tools, but when you know at compile time whatall the things are you're looking for, regular expressions bring far more generality and flexibility than you need.

"Encouraging regular expressions as a panacea for all text processing problems is not only lazy and poor engineering, it also reinforces their use by people who shouldn't be using them at all."

http://commandcenter.blogspot.com/2011/08/regular-expression...

http://news.ycombinator.com/item?id=2915137

Personally, I think they're pretty darn useful and too powerful to not learn, but Pike's comment makes me think that maybe they're also a crutch that I've relied on too much rather than learning enough about lexing/parsing.

Re: Is it a must for every programmer to learn regular expressions?

#56
post #8

Earlier quoted context omitted.

[1] Thinking of embedded systems developers creating code for, say, automotive entertainment systems, or control code for scientific or medical hardware. Don't they have log files to read?

That's what grep is for ;-) But the point is I can't imagine someone getting to that point without previously having learned it.

Err, grep uses regular expressions.

Re: Is it a must for every programmer to learn regular expressions?

#57
post #56

Earlier quoted context omitted.

That's what grep is for ;-) But the point is I can't imagine someone getting to that point without previously having learned it.

Err, grep uses regular expressions.

You don't need to know regular expressions to use grep for reviewing log files though.

Re: Is it a must for every programmer to learn regular expressions?

#58

Yes, it is, without a doubt. It's one of the most universal tricks of the trade that you'll literally never regret learning, mainly because just about any environment you'll ever work in will by necessity support regexes, and in many, it will be the primary way you interact with text. But it's also a must that you realize that despite the fact that they're exceptionally useful and widely supported, regexes are a disg…

I could imagine a Jquery-like DSL where something like /^[A-Z]+[0-9]{2}$/ could be expressed like Match(str).BeginsWith().BigCaseAlpha().AtLeastOne().Numeric().FixedLength(2).EndsWith()

Of course, you would also have to be able to nest these for more advanced matching...

Is there something like this already in existence? :)

Re: Is it a must for every programmer to learn regular expressions?

#59
post #58

Yes, it is, without a doubt. It's one of the most universal tricks of the trade that you'll literally never regret learning, mainly because just about any environment you'll ever work in will by necessity support regexes, and in many, it will be the primary way you interact with text. But it's also a must that you realize that despite the fact that they're exceptionally useful and widely supported, regexes are a disg…

I could imagine a Jquery-like DSL where something like /^[A-Z]+[0-9]{2}$/ could be expressed like Match(str).BeginsWith().BigCaseAlpha().AtLeastOne().Numeric().FixedLength(2).EndsWith() Of course, you would also have to be able to nest these for more advanced matching... Is there something like this already in existence? :)

How about pyparsing? http://pyparsing.wikispaces.com/

Re: Is it a must for every programmer to learn regular expressions?

#60
post #17

Learn the 20% that you will use 80% of the time. If your not willing to do that your probably not serious about programming in the first place.

Amen to that. For reasons I can only guess at, I'm the "regex guy" at work. When people have a regex question they come to me. And the interesting thing is that the answers are not hard, well within the 20% area. I've found that most people don't care to learn how to use character classes, or even what they're for. For example, to match the single letter 'i', I've seen people write: [i] Many don't know the difference…

Learning about the nongreedy modifier is IMHO a must when it comes to regexps. It makes them much more convenient to write.
Post reply on HN