0

I need to implement positive and negative ranges in my regex matcher.

It looks not difficult for positive range:

[1-3] == (1|2|3)

But I do not understand how to convert negative range [^1-3] to simple regex string.

Is it possible?

Thanks!


Update

Not. Seems it is impossible.

Ok, how regex libraries process negative ranges in this case?

Vitaly
  • 2,552
  • 2
  • 18
  • 21
  • Well, the problem does not related to language. One can test regex in many open libraries and web services. And negative range works where. I need to know how it works. – Vitaly Jan 11 '16 at 16:50
  • I misunderstood what you were talking about and deleted my comment. .Net lets you do character class subtraction. – shawnt00 Jan 11 '16 at 16:50
  • Possibly, with lookahead/lookbehind assertions. It depends on which regexp engine you use, thus the language you use, and what you're trying to match exactly. – Vincent Savard Jan 11 '16 at 16:51
  • It is unclear what you are asking. Are you trying to implement a regex engine? – ndnenkov Jan 11 '16 at 16:52
  • >> Are you trying to implement a regex engine? Yes – Vitaly Jan 11 '16 at 16:53
  • What tools are you using? – ndnenkov Jan 11 '16 at 16:55
  • >> What tools are you using? No tools. My own implementations of Thompson algorithm with | , * , + , ? , {n,m}, () support – Vitaly Jan 11 '16 at 16:58

1 Answers1

1

If the regex engine you're using supports negative lookahead, you can do it like this:

(?!1|2|3).

?! is the negative lookahead operator. It says "the characters that follow this expression must not match this expression." It makes a negative match without advancing the cursor. Here it's followed by a . to indicate any character.

Jordan Running
  • 102,619
  • 17
  • 182
  • 182
  • This is not what OP is asking for IMO. He wants to know how to represent negated values of edges in NFAs. – ndnenkov Jan 11 '16 at 17:04