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,747

6 Answers6

17

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
  • 148,830
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,518
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
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,490
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

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