简体   繁体   English

匹配模式前面或后面都没有字符串

[英]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 stringend 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 正则表达式:匹配除特定模式之外的所有内容

Regex: Match everything but one word 正则表达式:匹配除一个单词以外的所有内容

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.

相关问题 仅当前面或后面没有另一个模式时才匹配模式 - Match a pattern only when NOT preceded or followed by another pattern 任何方式匹配模式EITHER先于OR后跟某个字符? - Any way to match a pattern EITHER preceded OR followed by a certain character? 正则表达式:匹配不紧随字母数字字符之后的字符串 - Regular Expression: Match a string not immediately preceded by or followed by a alphanumeric character Python Regex:匹配前面或后面没有带数字的单词的字符串 - Python Regex: Match a string not preceded by or followed by a word with digits in it C#中的正则表达式:匹配在“ _”或“”或“”之前和/或之后的字符串 - Regex in C#: match string thats preceded and/or followed by “_” or “ ” or “” 仅在数字前或后没有数字时匹配 - Match only if not preceded or followed by a digit 匹配一些没有跟随和前面的东西 - match something not followed AND preceded by something 匹配模式前面没有字符 - Match pattern not preceded by character 如何在VBS中使用正则表达式模式来匹配没有在换行符或回车符之前或之后的逗号? - How do I use a regex pattern in VBS to match commas not preceded or followed by a line feed or carriage return? 正则表达式(python)仅在特定模式之前或之后匹配同一组多次 - Regex (python) to match same group several times only when preceded or followed by specific pattern
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM