Live data from Hacker News

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

programmers.stackexchange.com

41–50 of 60 posts

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

#41
I think every programmer should have an understanding of what Regular Expressions are, how they can be used, and where to find a cheat sheet or reference (like www.regular-expressions.info).

It helps to know the basic syntax by memory, but you can just as easily look it up if you understand how they work.

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

#42
post #37
post #22

Earlier quoted context omitted.

A typical regex looks like this: \b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b Which is also what happens when a cat walks across the keyboard.

I find that perfectly readable, except for the \b which I hadn't seen before. It's matching an all-uppercase email address.

There's plenty of upper case e-mail addresses that won't match that expression.

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

#43
post #22

Earlier quoted context omitted.

How would you design a regular expression syntax more intuitively? Personally, I find beauty and simplicity in regular expressions. Sure, they can grow to hideous atrocities, but you can achieve such disastrous feats with any language/syntax. Maybe you could back up your claim of regexes being a disgusting abomination with, at the very least, anecdotal evidence.

A typical regex looks like this: \b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b Which is also what happens when a cat walks across the keyboard.

There's nothing wrong with regex syntax. But there _is_ something wrong with the formatting of your example: it's not readable. Perhaps you _should_ write (assuming Java-syntax):

  (?x:              #standard token is an uppercase letter, digit, dot, or hyphen
    \b
    [A-Z0-9._%-]+   #1 or more of standard token, underscore, or percent sign
    @               #at-sign
    [A-Z0-9.-]+     #1 or more standard tokens
    \.              #dot
    [A-Z]{2,4}      #2 to 4 uppercase letters
    \b
  )
We can easily optimize for readability with regex syntax.

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

#44
post #37

Earlier quoted context omitted.

I find that perfectly readable, except for the \b which I hadn't seen before. It's matching an all-uppercase email address.

There's plenty of upper case e-mail addresses that won't match that expression.

/i

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

#45

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…

Please name 2 concrete ways to improve the "interface" of a standard regex.

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

#46
post #22

Earlier quoted context omitted.

How would you design a regular expression syntax more intuitively? Personally, I find beauty and simplicity in regular expressions. Sure, they can grow to hideous atrocities, but you can achieve such disastrous feats with any language/syntax. Maybe you could back up your claim of regexes being a disgusting abomination with, at the very least, anecdotal evidence.

A typical regex looks like this: \b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b Which is also what happens when a cat walks across the keyboard.

You can say that about pretty much anything if you aren't familiar with the syntax. That looks pretty readable to me - certainly far more portable and readable than equivalent code.

[It's also perfectly obvious that if this is an attempt to match email addresses that it's not a very good one - but I don't know the context where it's supposed to be used, it might be good enough for whatever the author intended].

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

#47

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…

Hmm - you know I actually think it's not correct to look at regex as an interface - even though we use it as one. It's really more accurate to look at it as a grammar (type 3 if I remember correctly). Anything that comes out of the whole chomskian hierarchy stuff isn't going to look intuitive. But the point is that it is a particular, very rigorously defined system of representation. And various systems of representa…

> These things just are - written into the laws of the world. They are discovered - not invented.

This applies only to the theoretical computer science regexes. The practical regexes are very different in this aspect.

Practical regexes are neither discovered nor invented - they are constructed. The theoretical regexes are just the basis but on top of that there's a lot of features added. Some of them are just syntactic sugar for theoretical regexes but others actually make the language non-regular.

Groups that match what previous named groups have matched are definitely in this category.

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

#48
post #37

Earlier quoted context omitted.

I find that perfectly readable, except for the \b which I hadn't seen before. It's matching an all-uppercase email address.

There's plenty of upper case e-mail addresses that won't match that expression.

Well, if you want to be fully compliant you can go for this 6kb monster

http://ex-parrot.com/~pdw/Mail-RFC822-Address.html

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

#49

Earlier quoted context omitted.

There's plenty of upper case e-mail addresses that won't match that expression.

/i

There's characters missing[1] and the tld is too short[2]. And even if that's fixed, we still don't match internationalized addresses or actually validate that the e-mail address exists. You're probably better off with something like...

   if "@" in email and "." in email.split("@")[1]:
       send_verification(email)
...but you should probably also check for common misspellings like "gmial.com" etc.

[1] http://en.wikipedia.org/wiki/Email_address#Syntax [2] http://en.wikipedia.org/wiki/List_of_Internet_top-level_doma...

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

#50
You can get by as a programmer without knowing regexes. You can also get by without knowing about pointers. Heck, maybe you can even get by without knowing SQL or OOP.

But you will be limited. You will run into a lot of situations where your ignorance will create friction and limitations. Something as simple as editing an nginx configuration file, for example.

You should learn regexes, you should be able to use a regex as necessary and be able to understand regexes you come across, you should understand when they are most useful, and when they are not. You should learn their limits and your limits in using them as well. When used appropriately they can be a potent addition to your technical knowledge. When they should be used but are avoided the result is typically a massive inflation of the effort to solve a simple problem. And when used incorrectly they can result in an inflation of intractable complexity (much like any technology).

Post reply on HN