简体   繁体   English

Java Regex否定提前错误匹配

[英]Java Regex negative lookahead wrong match

I'm looking for strings where the two first digits are present (in any order) in the digits that follow the space character.First I tried 我正在寻找在空格字符后面的数字中出现前两个数字(以任意顺序)的字符串。

(\d)(\d)\s\d*(\1|\2)\d*[\1\2&&[^\3]][\d]*

but it seems that I can't use brackets with backreferences.I tried using the lookahead feature instead with 但似乎我不能将括号与反向引用一起使用。我尝试使用先行功能来代替

(\d)(\d)\s\d*(\1|\2)\d*(?!\3(\1|\2))\d*

but I isn't right.The idea was "look for two digits, followed by a space, followed by zero or more digits, followed by either of the captured digits, followed by zero or more digits, followed by one of the captured digits which ISN'T the one I got before, followed by zero or more digits".21 20329 is a match.Why?How do I look for the strings I need? 但是我不对。这个想法是“寻找两个数字,后跟一个空格,然后是零个或多个数字,然后是一个捕获的数字,然后是零个或多个数字,然后是一个捕获的数字我之前得到的不是哪个,后面跟零个或多个数字。” 21 20329是一个匹配项。为什么?如何查找所需的字符串?

This is simpler. 这比较简单。

^(\d)(\d) (?=.*?\1)(?=.*?\2)\d+

See demo 观看演示

  1. The first lookahead ensures that the digit captured by Group 1 is present somewhere later in the string. 先行先行确保由组1捕获的数字出现在字符串的后面。
  2. The second lookahead ensures that the digit captured by Group 2 is present somewhere later in the string. 第二次前瞻确保第二组捕获的数字出现在字符串的后面。
  3. If these conditions are met, the \\d+ eats up all the digits after the space. 如果满足这些条件,则\\d+吃掉空格后的所有数字。

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

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