Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.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_Optimization - Fatal编程技术网

Java 高效地查找正则表达式的所有重叠匹配

Java 高效地查找正则表达式的所有重叠匹配,java,regex,optimization,Java,Regex,Optimization,这是一个后续行动 有没有办法让这段代码更快 public static void allMatches(String text, String regex) { for (int i = 0; i < text.length(); ++i) { for (int j = i + 1; j <= text.length(); ++j) { String positionSpecificPattern = "((?<=^.{"+i+"})("

这是一个后续行动

有没有办法让这段代码更快

public static void allMatches(String text, String regex)
  {
    for (int i = 0; i < text.length(); ++i) {
      for (int j = i + 1; j <= text.length(); ++j) {
        String positionSpecificPattern = "((?<=^.{"+i+"})("+regex+")(?=.{"+(text.length() - j)+"}$))";
        Matcher m = Pattern.compile(positionSpecificPattern).matcher(text);

        if (m.find()) 
        {   
          System.out.println("Match found: \"" + (m.group()) + "\" at position [" + i + ", " + j + ")");
        }   
      }   
    }   
  }
publicstaticvoidallmatches(字符串文本、字符串正则表达式)
{
对于(int i=0;i对于(int j=i+1;j在另一个问题中,您提到了Matcher的
region()
方法,但您没有充分利用它。它如此有价值的是,锚定将在区域的边界处匹配,就像它们是独立字符串的边界一样。这是假设您拥有
useAnchoringBounds()
选项集,但这是默认设置

public static void allMatches(String text, String regex)
{
  Matcher m = Pattern.compile(regex).matcher(text);
  int end = text.length();
  for (int i = 0; i < end; ++i)
  {
    for (int j = i + 1; j <= end; ++j) 
    {
      m.region(i, j);

      if (m.find()) 
      {   
        System.out.printf("Match found: \"%s\" at position [%d, %d)%n",
                          m.group(), i, j);
      }   
    }   
  }   
}
…我得到这个输出:

Match found: "04" at position [11, 13)
Match found: "31" at position [14, 16)
Match found: "41" at position [17, 19)
Match found: "412" at position [17, 20)
Match found: "12" at position [18, 20)
Match found: "55" at position [21, 23)
Match found: "555" at position [21, 24)
Match found: "55" at position [22, 24)
Match found: "12" at position [25, 27)
Match found: "123" at position [25, 28)
Match found: "1235" at position [25, 29)
Match found: "23" at position [26, 28)
Match found: "235" at position [26, 29)
Match found: "35" at position [27, 29)

太棒了,非常感谢!是的,我认为添加
^
$
锚定会有所帮助。事实证明,出于我的目的,我还需要
使用透明边界(true)
。太棒了!
Match found: "04" at position [11, 13)
Match found: "31" at position [14, 16)
Match found: "41" at position [17, 19)
Match found: "412" at position [17, 20)
Match found: "12" at position [18, 20)
Match found: "55" at position [21, 23)
Match found: "555" at position [21, 24)
Match found: "55" at position [22, 24)
Match found: "12" at position [25, 27)
Match found: "123" at position [25, 28)
Match found: "1235" at position [25, 29)
Match found: "23" at position [26, 28)
Match found: "235" at position [26, 29)
Match found: "35" at position [27, 29)