1

Let's say that I have the following words that I want to accept in a regular expression. iare, sare, are, snore , zere, tnore

I could use the or expression going this way : (iare|sare|are|snore|zere|tnore) but trying to optimize it, I rewrote it as following : (s|t)?(t|n)?(i|s|a|z|t)?(a|o|e)re

Is there a way to write a better regex than the last one?

Any help would be appreciated. Thanks

HamZa
  • 14,671
  • 11
  • 54
  • 75
wmehanna
  • 394
  • 3
  • 15
  • Which regular expression engine are you using? – psxls Dec 01 '13 at 17:44
  • 5
    Against what criteria are you trying to optimize it? Performance? Readability? Portability? – Peter Alfvin Dec 01 '13 at 17:47
  • @hwnd one character shorter `([is]?a|[st]no|ze)re` :P – HamZa Dec 01 '13 at 17:52
  • The latter one would match `stsore` as well. In case you don't want that, the first one is better. – kqr Dec 01 '13 at 17:52
  • I'm not using any engine, just learning how to use regex for the moment. I'm using regexpal.com to test my rexpression. My criteria to optimise it would be more the readability. – wmehanna Dec 01 '13 at 17:53
  • @user3054950 if it's readability, then just stick with your first expression. When it get's complicated, you might use the [`x` modifier](http://perldoc.perl.org/perlre.html#%2fx) to add space and comments. – HamZa Dec 01 '13 at 17:55
  • Thanks HamZa, you're 2 answers are much appreciated. – wmehanna Dec 01 '13 at 18:00
  • @user3054950 See [this small demo](http://regex101.com/r/nA6sP7). Learn regex the [normal way](http://www.regular-expressions.info/tutorial.html) and the [easy way](http://regex.learncodethehardway.org/book/). You might even [play](http://regexcrossword.com) a bit or [chat](http://chat.stackoverflow.com/rooms/25767/regex) about it :) – HamZa Dec 01 '13 at 18:08
  • @wmehanna if your objective is "more the readability", the answer is obvious just from reading your question! – Christophe Dec 01 '13 at 19:12
  • @Christophe I noticed that when I read myself after posting it. I guess now i'm more looking into performance. – wmehanna Dec 01 '13 at 21:10
  • @wmehanna for performance, the best option is often to... avoid using regex! For example, see here some alternate options in JavaScript (upvoted answers) http://stackoverflow.com/questions/13737091/concise-way-to-compare-against-multiple-values/ – Christophe Dec 01 '13 at 22:05

1 Answers1

1

Here are two possible optimizations that will match the same as your expression:

(?:ze|[is]?a|[st]no)re

and

(?:s(?:no|a)|i?a|tno|ze)re)

zx81
  • 41,100
  • 9
  • 89
  • 105