0

I have a regex matching a 2 digit number ^(\d{2})$, but need to do the opposite of this. For example, the following need to match:

HK
VK
112
1H
V1

But the following would not match

14
15
91
00
99

Is there any way I can just invert my regex? I know I could check for a match and invert the result, but that is not possible within my constraints.

Troy Cosentino
  • 4,658
  • 9
  • 37
  • 59
  • 3
    It is easy if your regex engine supports lookaheads - `^(?!\d{2}$).*$`. See http://stackoverflow.com/a/37988661/3832970 (*a string equal to some string* section). – Wiktor Stribiżew Oct 19 '16 at 17:23
  • And if it doesn't, you're gonna have a bad time. `^(?:.?|.{3,}|[^\d].|.[^\d])$` – Siguza Oct 19 '16 at 17:27
  • 1
    It is important for people to know whether these occur inside other strings or not. The current suggestions you've received have the "start of string" anchor, and this will only work in that case. For regex it is always important to understand the context of where the matches need to occur. – gview Oct 19 '16 at 17:28
  • Also important to know your regex flavor/tool – anubhava Oct 19 '16 at 18:35

1 Answers1

-1

You can try this:

^([^\d]{2}|\d[^\d]|[^\d]\d|.|.{3}.*)$
mm759
  • 1,404
  • 1
  • 9
  • 7
  • Why was this downvoted? Doesn't it work? I don't see a much easier approach for engines that don't support lookahead. – mm759 Oct 20 '16 at 15:37