0

I need to match any sequence of 9 digits or more, that is not preceded OR followed by 2 uppercase letters, anywhere in a text:

OG237338070BR // should NOT match
og237338070br // should match
oG237338070bR // should match
G237338070BR  // should match
OG237338070B  // should match
G237338070B   // should match
asd OG237338070BR asd    // should NOT match
asd G237338070BR asd     // should match
asd OG237338070B asd     // should match
asd OG237338070Basd asd  // should match
asd OG237338070BRasd asd // should NOT match


I've tried unsuccessfully the following:

(?![A-Z]{2}(\d{9,})[A-Z]{2})(\d{9,})

This one ignores the negative lookahead, simply because it can start anywhere


With a combination of Negative Lookahead and Negative Lookbehind, I was able to have a AND operation working on, but not a OR operation:

(?<![A-Z]{2})(\d{9,})(?![A-Z]{2})

This one only matches if not preceded by 2 uppercase letters AND not followed by 2 uppercase letters


So, the question is: is it possible with just Regex?


Info:

1 - The target engine is .Net, so I'm able to use variable length negative lookbehind.

2 - I can't use start string and end string anchors (at least, I think that I can't), because my match could be anywhere in the string, and could even happen multiple occurrences on a same string(text).

3 - This is not a duplicate. I could not found anywhere the OR condition with preceded and followed sequences/strings. The closest that I found was someone that was trying to match a pattern not followed by a character, and He/She could make use of start string and end string anchors.

4 - What I already found and tried:

Match only if not preceded or followed by a digit

javascript regex, word not followed and not preceded by specific char

Regular Expression - Match String Not Preceded by Another String (JavaScript)

Match pattern not preceded by character

Regex match exact word not preceded or followed by other characters

https://superuser.com/questions/477463/is-it-possible-to-use-not-in-a-regular-expression-in-textmate

Regex: match everything but specific pattern

Regex: Match everything but one word

Vitox
  • 2,208
  • 16
  • 17

1 Answers1

1

With your second attempt, that performs a logical AND, you are almost there. Just use | to separate the two possible scenarios:

(?<![A-Z]{2})(\d{9,})|(\d{9,})(?![A-Z]{2})

trincot
  • 211,288
  • 25
  • 175
  • 211
  • 1
    Perfect! Looking now, it's so obvious, but I could not saw the OR possibility by splitting the pattern in "half". Until now. Thanks! – Vitox Jan 25 '20 at 10:14