Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression: find range except for one letter or a range

Tags:

regex

php

How can I use the negation within square brackets as an exception, to find e. g. everything between a-z except for the the range from m-o? [a-z^m-o]?

By the way: it's not for the sake of this example that I ask, but to be able to exclude ranges within ranges, or even single letters within ranges. I am pretty much aware that in this example it can be calculated.

I use the Zend engine (PHP).

like image 314
arik Avatar asked Jul 29 '10 14:07

arik


2 Answers

You should be able calculate the difference yourself.

[a-lp-z]

If the regex engine supports lookahead assertion, you could use

(?![m-o])[a-z]

but this would probably be less efficient.

like image 64
kennytm Avatar answered Sep 28 '22 01:09

kennytm


In addition to what Kenny mentions:

  1. The JDK (at least) supports this syntax:

    [a-z&&[^m-o]]

  2. A couple of engines (including the .NET framework) support this:

    [a-z-[m-o]]

like image 25
ig0774 Avatar answered Sep 28 '22 01:09

ig0774