13

I've recently learnt regular expressions and I love writing / using them. I'm looking for ideas and more opportunities to use them, however I don't want to overuse them as an all purpose tool, as often warned by people.

What kind of things should I use regular expressions for, and where should I not use them? (Apart from the obvious: HTML parsing).

Click Upvote
  • 2,707
  • 3
  • 26
  • 34
  • 6
    Required quote: _Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems._ (but see http://www.codinghorror.com/blog/2008/06/regular-expressions-now-you-have-two-problems.html for context) – Steven A. Lowe May 08 '11 at 05:59
  • 1
    There is only one thing you can do with regular expressions: recognize words of regular languages. It's that easy. For this reason it is a must for a programmer to have some notion of the language classes and to be able to identify regular languages. – Ingo May 08 '11 at 08:41
  • 2
    Basically you don't want to use regular expressions when you want to use an `if` in it. –  May 08 '11 at 13:30

6 Answers6

16

Most string matching tasks can benefit from regular expressions. When you stop calling it matching and start calling it parsing, regular expressions aren't so helpful. Usually that's because people tend to call it parsing when it involves nesting or other recursion that regular expressions can't handle.

Probably the place I use it most, where some people aren't aware they are available, is in my editor while searching for something. They don't make it into my code near as often as they help my coding go faster.

Karl Bielefeldt
  • 146,727
  • 38
  • 279
  • 479
4

My answer is in the negative: Don't try to write a parser (for example, for HTML) with regular expressions. You can extract all kinds of useful information with them, but there are some things you can't do. I consider understanding regexps on a par with SQL, C, and Python — it's one of the building blocks of modern computing.

Peter Rowell
  • 7,468
  • 2
  • 29
  • 33
  • C and Python aren't a building block of modern computing ;) – Click Upvote May 08 '11 at 05:56
  • 1
    +1 for being the first to link to, well, *that*...that *thing*. – Jon Purdy May 08 '11 at 06:52
  • 1
    @Click: Actually, they are. Try doing anything in the Linux kernel without a solid grounding in C. And as for Python ... IMNSHO, based on my experience with well over 30 languages, it beats the crap out of whatever language is in second place. – Peter Rowell May 08 '11 at 20:44
4

I would say one tell tale sign that a regex won't work is when you need something that can be nested. E.g. a programming language (or HTML/XML/etc.). Once you start nesting then you need to store the state and use a state machine.

Also if you look at the regex for e-mail here you can see that Regexes can quickly get unreadable. Sometimes even though you can use Regular Expressions, using a grammar makes things more clear. Even with simpler regexes, you can quickly start making something that is hard to read/maintain.

Additionally there are many tasks that do not require regex. For example, you could split a string of comma separated fields by using a regular expression, but it is much simpler just to say string.split(","). Generally a regex will require multiple steps/searches while split will do it in one statement. Also for a simple search it will be more efficient and more clear to use a built in search routine.

Cervo
  • 1,748
  • 16
  • 16
  • 1
    As an aside, you can use regex on HTML if you don't need to parse the whole thing...E.g. if there is a table on the page in a certain format, you can probably use a regex to parse that quickly. But if you need to understand an html document that can be arbitrarily nested...forget it. E.g. is that tag in a comment, or wrongly inside a tab in the body? You can't tell... Really for one or two layers of nesting you could make a regex. The true issue is arbitrary nesting...you just can't make an expression for that in the general case... – Cervo May 08 '11 at 05:36
4

Where they shine is where you have a definition for a string that is well defined and straightforward, so you can both (a) verify that a piece of input matches the pattern and (b) extract all the parts of the pattern from that input, in a single regex operation.

For instance, just the other day I needed to deal with certain codes than consisted of two single-letter identifiers (one of which had three options, the other could be any alpha), a date, and then a two digit number, like this: MR_20110508_01

One straightforward regex with 4 named groups enabled me to do a single call which both checked that an incoming code was valid, and gave me 4 named groups that I could access to pull out the 4 pieces.

The more arbitrary content the target of the regex can contain, or the more rules that depend on other parts of the content, the more likely you are to be heading into Jamie Zawinski's "now you have two problems" hell.

Carson63000
  • 10,510
  • 1
  • 28
  • 50
3

I notice that the slash between can and should in your question does not underline the most important part of it: the difference between the two.

There are things that can be done with regexps that should not be done with them. One example is using the following regexp:

^([07]|6[29]*3|(5|6[29]*[18])(4|5[29]*[18])*(6|5[29]*3)|(4|6[29]*[07]|(5|6[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*([29]|[18][29]*3|([07]|[18][29]*[18])(4|5[29]*[18])*(6|5[29]*3))|(3|6[29]*6|(5|6[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|(4|6[29]*[07]|(5|6[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))([18]|4[29]*6|(3|4[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))*(5|4[29]*3|(3|4[29]*[18])(4|5[29]*[18])*(6|5[29]*3)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*([29]|[18][29]*3|([07]|[18][29]*[18])(4|5[29]*[18])*(6|5[29]*3)))|([29]|6[29]*5|(5|6[29]*[18])(4|5[29]*[18])*([18]|5[29]*5)|(4|6[29]*[07]|(5|6[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(4|[18][29]*5|([07]|[18][29]*[18])(4|5[29]*[18])*([18]|5[29]*5))|(3|6[29]*6|(5|6[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|(4|6[29]*[07]|(5|6[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))([18]|4[29]*6|(3|4[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))*([07]|4[29]*5|(3|4[29]*[18])(4|5[29]*[18])*([18]|5[29]*5)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(4|[18][29]*5|([07]|[18][29]*[18])(4|5[29]*[18])*([18]|5[29]*5))))(3|[07][29]*5|(6|[07][29]*[18])(4|5[29]*[18])*([18]|5[29]*5)|(5|[07][29]*[07]|(6|[07][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(4|[18][29]*5|([07]|[18][29]*[18])(4|5[29]*[18])*([18]|5[29]*5))|(4|[07][29]*6|(6|[07][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|(5|[07][29]*[07]|(6|[07][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))([18]|4[29]*6|(3|4[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))*([07]|4[29]*5|(3|4[29]*[18])(4|5[29]*[18])*([18]|5[29]*5)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(4|[18][29]*5|([07]|[18][29]*[18])(4|5[29]*[18])*([18]|5[29]*5))))*([18]|[07][29]*3|(6|[07][29]*[18])(4|5[29]*[18])*(6|5[29]*3)|(5|[07][29]*[07]|(6|[07][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*([29]|[18][29]*3|([07]|[18][29]*[18])(4|5[29]*[18])*(6|5[29]*3))|(4|[07][29]*6|(6|[07][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|(5|[07][29]*[07]|(6|[07][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))([18]|4[29]*6|(3|4[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))*(5|4[29]*3|(3|4[29]*[18])(4|5[29]*[18])*(6|5[29]*3)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*([29]|[18][29]*3|([07]|[18][29]*[18])(4|5[29]*[18])*(6|5[29]*3))))|([18]|6[29]*4|(5|6[29]*[18])(4|5[29]*[18])*([07]|5[29]*4)|(4|6[29]*[07]|(5|6[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(3|[18][29]*4|([07]|[18][29]*[18])(4|5[29]*[18])*([07]|5[29]*4))|(3|6[29]*6|(5|6[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|(4|6[29]*[07]|(5|6[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))([18]|4[29]*6|(3|4[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))*(6|4[29]*4|(3|4[29]*[18])(4|5[29]*[18])*([07]|5[29]*4)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(3|[18][29]*4|([07]|[18][29]*[18])(4|5[29]*[18])*([07]|5[29]*4)))|([29]|6[29]*5|(5|6[29]*[18])(4|5[29]*[18])*([18]|5[29]*5)|(4|6[29]*[07]|(5|6[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(4|[18][29]*5|([07]|[18][29]*[18])(4|5[29]*[18])*([18]|5[29]*5))|(3|6[29]*6|(5|6[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|(4|6[29]*[07]|(5|6[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))([18]|4[29]*6|(3|4[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))*([07]|4[29]*5|(3|4[29]*[18])(4|5[29]*[18])*([18]|5[29]*5)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(4|[18][29]*5|([07]|[18][29]*[18])(4|5[29]*[18])*([18]|5[29]*5))))(3|[07][29]*5|(6|[07][29]*[18])(4|5[29]*[18])*([18]|5[29]*5)|(5|[07][29]*[07]|(6|[07][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(4|[18][29]*5|([07]|[18][29]*[18])(4|5[29]*[18])*([18]|5[29]*5))|(4|[07][29]*6|(6|[07][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|(5|[07][29]*[07]|(6|[07][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))([18]|4[29]*6|(3|4[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))*([07]|4[29]*5|(3|4[29]*[18])(4|5[29]*[18])*([18]|5[29]*5)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(4|[18][29]*5|([07]|[18][29]*[18])(4|5[29]*[18])*([18]|5[29]*5))))*([29]|[07][29]*4|(6|[07][29]*[18])(4|5[29]*[18])*([07]|5[29]*4)|(5|[07][29]*[07]|(6|[07][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(3|[18][29]*4|([07]|[18][29]*[18])(4|5[29]*[18])*([07]|5[29]*4))|(4|[07][29]*6|(6|[07][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|(5|[07][29]*[07]|(6|[07][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))([18]|4[29]*6|(3|4[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))*(6|4[29]*4|(3|4[29]*[18])(4|5[29]*[18])*([07]|5[29]*4)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(3|[18][29]*4|([07]|[18][29]*[18])(4|5[29]*[18])*([07]|5[29]*4)))))(5|3[29]*4|([29]|3[29]*[18])(4|5[29]*[18])*([07]|5[29]*4)|([18]|3[29]*[07]|([29]|3[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(3|[18][29]*4|([07]|[18][29]*[18])(4|5[29]*[18])*([07]|5[29]*4))|([07]|3[29]*6|([29]|3[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|([18]|3[29]*[07]|([29]|3[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))([18]|4[29]*6|(3|4[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))*(6|4[29]*4|(3|4[29]*[18])(4|5[29]*[18])*([07]|5[29]*4)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(3|[18][29]*4|([07]|[18][29]*[18])(4|5[29]*[18])*([07]|5[29]*4)))|(6|3[29]*5|([29]|3[29]*[18])(4|5[29]*[18])*([18]|5[29]*5)|([18]|3[29]*[07]|([29]|3[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(4|[18][29]*5|([07]|[18][29]*[18])(4|5[29]*[18])*([18]|5[29]*5))|([07]|3[29]*6|([29]|3[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|([18]|3[29]*[07]|([29]|3[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))([18]|4[29]*6|(3|4[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))*([07]|4[29]*5|(3|4[29]*[18])(4|5[29]*[18])*([18]|5[29]*5)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(4|[18][29]*5|([07]|[18][29]*[18])(4|5[29]*[18])*([18]|5[29]*5))))(3|[07][29]*5|(6|[07][29]*[18])(4|5[29]*[18])*([18]|5[29]*5)|(5|[07][29]*[07]|(6|[07][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(4|[18][29]*5|([07]|[18][29]*[18])(4|5[29]*[18])*([18]|5[29]*5))|(4|[07][29]*6|(6|[07][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|(5|[07][29]*[07]|(6|[07][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))([18]|4[29]*6|(3|4[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))*([07]|4[29]*5|(3|4[29]*[18])(4|5[29]*[18])*([18]|5[29]*5)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(4|[18][29]*5|([07]|[18][29]*[18])(4|5[29]*[18])*([18]|5[29]*5))))*([29]|[07][29]*4|(6|[07][29]*[18])(4|5[29]*[18])*([07]|5[29]*4)|(5|[07][29]*[07]|(6|[07][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(3|[18][29]*4|([07]|[18][29]*[18])(4|5[29]*[18])*([07]|5[29]*4))|(4|[07][29]*6|(6|[07][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|(5|[07][29]*[07]|(6|[07][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))([18]|4[29]*6|(3|4[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))*(6|4[29]*4|(3|4[29]*[18])(4|5[29]*[18])*([07]|5[29]*4)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(3|[18][29]*4|([07]|[18][29]*[18])(4|5[29]*[18])*([07]|5[29]*4)))))*(4|3[29]*3|([29]|3[29]*[18])(4|5[29]*[18])*(6|5[29]*3)|([18]|3[29]*[07]|([29]|3[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*([29]|[18][29]*3|([07]|[18][29]*[18])(4|5[29]*[18])*(6|5[29]*3))|([07]|3[29]*6|([29]|3[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|([18]|3[29]*[07]|([29]|3[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))([18]|4[29]*6|(3|4[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))*(5|4[29]*3|(3|4[29]*[18])(4|5[29]*[18])*(6|5[29]*3)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*([29]|[18][29]*3|([07]|[18][29]*[18])(4|5[29]*[18])*(6|5[29]*3)))|(6|3[29]*5|([29]|3[29]*[18])(4|5[29]*[18])*([18]|5[29]*5)|([18]|3[29]*[07]|([29]|3[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(4|[18][29]*5|([07]|[18][29]*[18])(4|5[29]*[18])*([18]|5[29]*5))|([07]|3[29]*6|([29]|3[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|([18]|3[29]*[07]|([29]|3[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))([18]|4[29]*6|(3|4[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))*([07]|4[29]*5|(3|4[29]*[18])(4|5[29]*[18])*([18]|5[29]*5)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(4|[18][29]*5|([07]|[18][29]*[18])(4|5[29]*[18])*([18]|5[29]*5))))(3|[07][29]*5|(6|[07][29]*[18])(4|5[29]*[18])*([18]|5[29]*5)|(5|[07][29]*[07]|(6|[07][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(4|[18][29]*5|([07]|[18][29]*[18])(4|5[29]*[18])*([18]|5[29]*5))|(4|[07][29]*6|(6|[07][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|(5|[07][29]*[07]|(6|[07][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))([18]|4[29]*6|(3|4[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))*([07]|4[29]*5|(3|4[29]*[18])(4|5[29]*[18])*([18]|5[29]*5)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(4|[18][29]*5|([07]|[18][29]*[18])(4|5[29]*[18])*([18]|5[29]*5))))*([18]|[07][29]*3|(6|[07][29]*[18])(4|5[29]*[18])*(6|5[29]*3)|(5|[07][29]*[07]|(6|[07][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*([29]|[18][29]*3|([07]|[18][29]*[18])(4|5[29]*[18])*(6|5[29]*3))|(4|[07][29]*6|(6|[07][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|(5|[07][29]*[07]|(6|[07][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))([18]|4[29]*6|(3|4[29]*[18])(4|5[29]*[18])*([29]|5[29]*6)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*(5|[18][29]*6|([07]|[18][29]*[18])(4|5[29]*[18])*([29]|5[29]*6)))*(5|4[29]*3|(3|4[29]*[18])(4|5[29]*[18])*(6|5[29]*3)|([29]|4[29]*[07]|(3|4[29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))(6|[18][29]*[07]|([07]|[18][29]*[18])(4|5[29]*[18])*(3|5[29]*[07]))*([29]|[18][29]*3|([07]|[18][29]*[18])(4|5[29]*[18])*(6|5[29]*3))))))*$

which matches decimal numbers divisible by 7 (and only those; source).

F'x
  • 269
  • 2
  • 9
  • 4
    Do you have a regular expression to parse that regular expression? Because I'm unable to do so. – svick May 08 '11 at 14:02
  • 1
    @svick Arbitrary regular expressions can not be parsed by regular expressions. But I figure you knew that already. – Justsalt Jul 02 '14 at 01:46
2

There are no hard and fast rules here but I would say any text possessing task that doesn't have a library available is ripe for regex's. It also depends heavily on the regex support os the language you are using. Perl has extensive support for regexs while java has less so. Therefore you would expect to use regexs more in perl than java.

ennuikiller
  • 1,168
  • 7
  • 8
  • 1
    The Java regex support is not that bad. It's arguably not as good as in perl, yet it's okay 99.99% of the time. – Ingo May 08 '11 at 08:38