[英]Match pattern not preceded or followed by string
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:我需要匹配任何 9 位或更多数字的序列,该序列前面没有OR后跟 2 个大写字母,文本中的任何位置:
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:结合 Negative Lookahead 和 Negative Lookbehind,我可以进行 AND 运算,但不能进行 OR 运算:
(?<![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如果前面没有 2 个大写字母且后面没有 2 个大写字母,则此匹配项仅匹配
So, the question is: is it possible with just Regex?所以,问题是:是否可以仅使用 Regex?
Info:信息:
1 - The target engine is .Net, so I'm able to use variable length negative lookbehind. 1 - 目标引擎是 .Net,所以我可以使用可变长度负向后视。
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). 2 - 我不能使用
start string
和end string
锚点(至少,我认为我不能),因为我的匹配可以在字符串中的任何地方,甚至可能在同一个字符串(文本)上多次出现。
3 - This is not a duplicate. 3 - 这不是重复的。 I could not found anywhere the OR condition with preceded and followed sequences/strings.
我在任何地方都找不到带有前后序列/字符串的 OR 条件。 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: 4 - 我已经发现并尝试过的:
Match only if not preceded or followed by a digit 仅当前面或后面没有数字时才匹配
javascript regex, word not followed and not preceded by specific char javascript正则表达式,单词后面没有,前面没有特定的字符
Regular Expression - Match String Not Preceded by Another String (JavaScript) 正则表达式 - 匹配前面没有另一个字符串的字符串 (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 https://superuser.com/questions/477463/is-it-possible-to-use-not-in-a-regular-expression-in-textmate
Regex: match everything but specific pattern 正则表达式:匹配除特定模式之外的所有内容
With your second attempt, that performs a logical AND, you are almost there.进行第二次尝试,即执行逻辑与,您就快到了。 Just use
|
只需使用
|
to separate the two possible scenarios:区分两种可能的情况:
(?<![AZ]{2})(\\d{9,})|(\\d{9,})(?![AZ]{2})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.