简体   繁体   English

正则表达式匹配除此正则表达式之外的所有内容

[英]Regex to match everything except this regex

I think this is a simple thing for a lot of you, but I have a very limited knowlegde of regex at the moment. 我认为对很多人来说这是一件简单的事情,但目前我对正则表达式的知识非常有限。 I want to match everything except a double digit number in a string. 我希望匹配除字符串中的两位数字之外的所有内容。

For example: 例如:

TEST 22 KLO4567 测试 22 KLO4567

QE 45 C2C QE 45 C2C

LOP 10 G7G400 LOP 10 G7G400

Now I found out the regex to match the double digit numbers: \\d{2} 现在我发现正则表达式匹配两位数字:\\ d {2}

Which matches the following: 符合以下条件:

TEST 22 KLO4567 测试22 KLO4567

QE 45 C2C QE 45 C2C

LOP 10 G7G400 LOP 10 G7G400

Now it seems to me that it would be fairly easy to turn that regex around to match everything BUT "\\d{2}". 现在在我看来,将这个正则表达式转换为匹配所有内容但是“\\ d {2}”相当容易。 I searched a lot but I can't seem to get it done. 我搜索了很多,但我似乎无法完成它。 I hope someone here can help. 我希望这里有人可以提供帮助。

This only works if your regex engine supports look behinds: 这仅适用于正则表达式引擎支持后面的内容:

^.+?(?=\d{2})|(?<=\d{2}).+$

Explanation: 说明:

The | | separates two cases where this would match: 将两种情况分开:

  • ^.+?(?=\\d{2}) ^ +?(?= \\ d {2})

This matches everything from the start of the string ( ^ ) until \\d{2} is encountered. 这匹配从字符串开头( ^ )到遇到\\d{2}内容。

  • (?<=\\d{2}).+$ (?<= \\ d {2})。+ $

This matches the end of the string, from the place just after two digits. 这匹配字符串的结尾,从两位数后面的位置。

If your regex engine doesn't support look behinds (JavaScript for example), I don't think it is possible using a pure regex solution. 如果你的正则表达式引擎不支持外观(例如JavaScript),我认为使用纯正则表达式解决方案是不可能的。

You can match the first part: 你可以匹配第一部分:

^.+?(?=\d{2})

Then get where the match ends, add 2 to that number, and get the substring from that index. 然后获取匹配结束的位置,将2添加到该数字,并从该索引获取子字符串。

You are right rejecting a search in regex is usually rather tricky. 你正确拒绝正则表达式中的搜索通常是相当棘手的。

In your case I think you want to have [^\\d{2}] , however, this is tricky as your other strings also contain two digits so your regex using it won't select them. 在你的情况下,我认为你想要[^\\d{2}] ,但是,这很棘手,因为你的其他字符串也包含两位数,所以使用它的正则表达式不会选择它们。

I would go with this regex (using PCRE 8.36 but should work also in others): 我会使用这个正则表达式(使用PCRE 8.36但也应该在其他人工作):

\\*{2}\\w*\\*{2}

Explanation: 说明:

\\*{2} .... matches "*" literally exactly two times \\*{2} ....精确地匹配“*”两次
\\w* .... matches "word character" zero or unlimited times \\w* ....匹配“单词字符”零或无限次

Found one regex pretty straightforward : 找到一个正则表达式很简单:

^(.*?[^\d])\d{2}([^\d].*?)$

Explanations : 解释:

  • ^ : matches the beginnning of a line ^:匹配行的开头
  • (.*?[^\\d]) : matches and catches the first part before the two numbers. (。*?[^ \\ d]):匹配并捕获两个数字之前的第一部分。 It can contain anything (.*?) but needs to end with something different to a number ([^\\d]) so we ensure that there is only 2 numbers in the middle 它可以包含任何东西(。*?),但需要以不同的数字([^ \\ d])结束,所以我们确保中间只有2个数字
  • \\d{2} : is the part you found yourself \\ d {2}:是你自己找到的部分
  • ([^\\d].*?) : is the symetric of (.*?[^\\d]) : begins with something different from a number ([^\\d]) and matches anything next. ([^ \\ d]。*?):是(。*?[^ \\ d])的对称:从不同于数字([^ \\ d])的东西开始,然后匹配任何东西。
  • $ : up to the end of the line. $:直到最后一行。

To test this reges you can use this link 要测试此reges,您可以使用此链接

It will match the first occurence of double digit, but because OP said there was only one it does the job correctly. 它将匹配第一次出现的两位数,但因为OP说只有一个它正确地完成了工作。 I expect it to work with every regex engine as nothing too complex is used. 我希望它可以与每个正则表达式引擎一起使用,因为没有太复杂的使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM