Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java正则表达式查找组没有明显的最大长度错误_Java_Regex - Fatal编程技术网

Java正则表达式查找组没有明显的最大长度错误

Java正则表达式查找组没有明显的最大长度错误,java,regex,Java,Regex,我知道java正则表达式不支持可变长度的look behinds,下面的代码应该会导致错误 (?<=(not exceeding|no((\\w|\\s)*)more than))xxxx (? java正则表达式不支持可变长度的look behind 这并非完全正确,Java支持有限的可变长度lookbehind,例如(?),这确实很奇怪。我找不到解释,但如果您将(\\w|\\s){0,30}更改为[\\w\\s]{0,30} Pattern.compile("(?<=(not

我知道java正则表达式不支持可变长度的look behinds,下面的代码应该会导致错误

(?<=(not exceeding|no((\\w|\\s)*)more than))xxxx
(?
java正则表达式不支持可变长度的look behind


这并非完全正确,Java支持有限的可变长度lookbehind,例如
(?),这确实很奇怪。我找不到解释,但如果您将
(\\w|\\s){0,30}
更改为
[\\w\\s]{0,30}

Pattern.compile("(?<=(not exceeding|no([\\w\\s]{0,30})more than))xxxx");
//BTW you don't need ^-----------------------------------------^ these parenthesis
//unless you want to use match from this group later
Pattern.compile(“(?)下面是一些测试用例(我删除了冗余参数,正如@Pshemo所提到的)。它只在查找包含子替换时失败。错误是

Look-behind group does not have an obvious maximum length near index 45
这里的关键词是“显而易见”

   import  java.util.regex.Pattern;
public class Test  {
   public static final void main(String[] ignored)  {
      test("(?<=not exceeding|no)xxxx");
      test("(?<=not exceeding|NOT EXCEEDING)xxxx");
      test("(?<=not exceeding|x{13})xxxx");
      test("(?<=not exceeding|x{12}x)xxxx");
      test("(?<=not exceeding|(x|y){12}x)xxxx");
      test("(?<=not exceeding|no(\\w|\\s){2,30}more than)xxxx");
      test("(?<=not exceeding|no(\\w|\\s){0,2}more than)xxxx");
      test("(?<=not exceeding|no(\\w|\\s){2}more than)xxxx");
   }
      private static final void test(String regex)  {
         System.out.print("testing \"" + regex + "\"...");
         try  {
            Pattern p = Pattern.compile(regex);
            System.out.println("Success");
         }  catch(Exception x)  {
            System.out.println(x);
         }

      }
}
import java.util.regex.Pattern;
公开课考试{
公共静态最终void main(忽略字符串[]){
测试(“(?Java Lookbehind是出了名的bug
所以你认为Java不支持无限查找

但是下面的模式将被编译

(?<=\d+)\w+
这一次的结果是你所期望的


但是,如果您稍微调整正则表达式以获得对而不是三元组,则使用
(?您尝试过的确切正则表达式是什么。您上面提到的正则表达式?Lookbehinds必须是零宽度,因此量词不是零宽度allowed@Swapnil为了简单起见,我删除了lookback中的一些关键字,xxxx是一个较长表达式的占位符,但我已经测试了该部分,这不是问题所在,请将其检查为per OP都失败了。我认为这两种方法都是可变长度的。这对我来说也很有效。奇怪。我以前在这个确切的上下文中使用过很多次括号,它从未失败过,但这一个似乎需要brackets@user2559503Java中的“向后看”的确是一种奇怪的生物。有时,即使很明显,它也无法计算出最大长度(如本例中所示),有时它允许使用无限制的正则表达式(如中所示)。“除非您以后希望从此组中进行匹配”如果lookbehind是一个组,你肯定不需要那些额外的参数。还没有测试过…@aliteralmind,但是lookbehind不算是捕获组,所以如果没有括号中的正则表达式,我们就无法得到与它匹配的部分。我决定赏金给你,因为你是原因之声:“不要浪费时间去理解bug"。虽然出了什么问题很有趣,但如果你已经有了可行的替代解决方案,就不值得花太多时间去弄清楚问题。@Pshemo谢谢你的赏金。我不久前也走了同样的路,问JG关于JS惊人的lookbehing行为,并得出了与你相同的结论……最后,如果是buggy,那是什么我们能做什么?这对我来说是正确的答案。我只是把lookback中的“+”改成了类似于“{1100}”的东西,事情按预期进行。
   import  java.util.regex.Pattern;
public class Test  {
   public static final void main(String[] ignored)  {
      test("(?<=not exceeding|no)xxxx");
      test("(?<=not exceeding|NOT EXCEEDING)xxxx");
      test("(?<=not exceeding|x{13})xxxx");
      test("(?<=not exceeding|x{12}x)xxxx");
      test("(?<=not exceeding|(x|y){12}x)xxxx");
      test("(?<=not exceeding|no(\\w|\\s){2,30}more than)xxxx");
      test("(?<=not exceeding|no(\\w|\\s){0,2}more than)xxxx");
      test("(?<=not exceeding|no(\\w|\\s){2}more than)xxxx");
   }
      private static final void test(String regex)  {
         System.out.print("testing \"" + regex + "\"...");
         try  {
            Pattern p = Pattern.compile(regex);
            System.out.println("Success");
         }  catch(Exception x)  {
            System.out.println(x);
         }

      }
}
testing "(?<=not exceeding|no)xxxx"...Success
testing "(?<=not exceeding|NOT EXCEEDING)xxxx"...Success
testing "(?<=not exceeding|x{13})xxxx"...Success
testing "(?<=not exceeding|x{12}x)xxxx"...Success
testing "(?<=not exceeding|(x|y){12}x)xxxx"...java.util.regex.PatternSyntaxException: Look-behind group does not
 have an obvious maximum length near index 27
(?<=not exceeding|(x|y){12}x)xxxx
                           ^
testing "(?<=not exceeding|no(\w|\s){2,30}more than)xxxx"...java.util.regex.PatternSyntaxException: Look-behind
group does not have an obvious maximum length near index 41
(?<=not exceeding|no(\w|\s){2,30}more than)xxxx
                                         ^
testing "(?<=not exceeding|no(\w|\s){0,2}more than)xxxx"...java.util.regex.PatternSyntaxException: Look-behind g
roup does not have an obvious maximum length near index 40
(?<=not exceeding|no(\w|\s){0,2}more than)xxxx
                                        ^
testing "(?<=not exceeding|no(\w|\s){2}more than)xxxx"...java.util.regex.PatternSyntaxException: Look-behind gro
up does not have an obvious maximum length near index 38
(?<=not exceeding|no(\w|\s){2}more than)xxxx
                                      ^
(?<=\d+)\w+
(?<=\\G\\d+,\\d+,\\d+),
0,123,45
6789,4,5
3,4,6000