Match pattern not preceded or followed by string

让人想犯罪 __ 提交于 2021-01-29 09:12:20

问题


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


回答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})



来源:https://stackoverflow.com/questions/59907883/match-pattern-not-preceded-or-followed-by-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!