Regular expression: find range except for one lett

2019-01-20 11:39发布

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]?

Thanks in advance!

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... ;) Once again, thanks in advance!

I use the Zend engine (PHP).

2条回答
在下西门庆
2楼-- · 2019-01-20 12:20

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]]

查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-20 12:33

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.

查看更多
登录 后发表回答