Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.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
使用regexjava获取重叠模式_Java_Regex_Algorithm_Overlap - Fatal编程技术网

使用regexjava获取重叠模式

使用regexjava获取重叠模式,java,regex,algorithm,overlap,Java,Regex,Algorithm,Overlap,此代码用于从数据集中提取连续字母 import java.util.regex.*; public class IFS { public static void main(String[] args) { String a; a = "ABC1abc"; regexchecker ("\\D+", a); } public static void regexchecker(String theRegex, Stri

此代码用于从数据集中提取连续字母

import java.util.regex.*;

public class IFS {

    public static void main(String[] args) {

        String a;
        a = "ABC1abc";

        regexchecker ("\\D+", a);
    }

    public static void regexchecker(String theRegex, String stuffToCheck) {
        // compiling the regex pattern
        Pattern checkRegex = Pattern.compile(theRegex);
        // the regex matcher being joined to the pattern
        Matcher regexmatcher = checkRegex.matcher(stuffToCheck);

        int end = stuffToCheck.length();
        for (int i = 0; i < end; i = i + 1) {

            for (int j = i + 1; j <= end; ++j) {

                regexmatcher.region(i, j);
                while (regexmatcher.find()) {
                    if (regexmatcher.group().length() != 0) {         

                        System.out.println(regexmatcher.group());
                    }
                }    
            }
        }
    }
}
import java.util.regex.*;
公共类国际单项体育联合会{
公共静态void main(字符串[]args){
字符串a;
a=“ABC1abc”;
regexchecker(“\\D+”,a);
}
公共静态void regexchecker(字符串theRegex,字符串stuff-tocheck){
//编译正则表达式模式
Pattern checkRegex=Pattern.compile(theRegex);
//正在加入模式的正则表达式匹配器
Matcher regexmatcher=checkRegex.Matcher(stuffToCheck);
int end=stuffToCheck.length();
对于(int i=0;i对于(int j=i+1;j由于您正在设置要在区域中检查的精确边界,因此您希望排除仅与区域的一部分匹配的匹配,因为它们将在不同的迭代中找到。由于默认情况下,Matcher在设置区域时将锚定边界应用于区域,因此在正则表达式中使用锚定来消除duplicate结果:

    regexchecker ("^\\D+$", a);

由于您正在设置要在区域中检查的精确边界,因此您希望排除仅匹配区域一部分的匹配,因为它们将在不同的迭代中找到。由于默认情况下,Matcher在设置区域时将锚定边界应用于区域,因此在正则表达式中使用锚定来消除重复结果:

    regexchecker ("^\\D+$", a);

我对您的代码进行了以下更改:

1. Defined an ArrayList to filter those duplicate matches. 
2. Small changes on start/end index to the matcher's region 

import java.util.regex.*;
import java.util.ArrayList;

public static void main(String[] args) {

    String a;
    a = "ABC1abc";

    regexchecker ("\\D+", a);
}

public static void regexchecker(String theRegex, String stuffToCheck) {
    // compiling the regex pattern
    Pattern checkRegex = Pattern.compile(theRegex);
    // the regex matcher being joined to the pattern
    Matcher regexmatcher = checkRegex.matcher(stuffToCheck);
    // define an ArrayList
    ArrayList<String> result = new ArrayList<>();

    int end = stuffToCheck.length();
    for (int i = 1; i <= end; i++) {

        for (int j = 0; j <= end-i; j++) {

            regexmatcher.region(j, j + i);
            while (regexmatcher.find()) {
                if (result.indexOf(regexmatcher.group()) == -1) {         

                    System.out.println(regexmatcher.group());
                    //result.add(regexmatcher.group());
                }
            }    
        }
    }
}

我对您的代码进行了以下更改:

1. Defined an ArrayList to filter those duplicate matches. 
2. Small changes on start/end index to the matcher's region 

import java.util.regex.*;
import java.util.ArrayList;

public static void main(String[] args) {

    String a;
    a = "ABC1abc";

    regexchecker ("\\D+", a);
}

public static void regexchecker(String theRegex, String stuffToCheck) {
    // compiling the regex pattern
    Pattern checkRegex = Pattern.compile(theRegex);
    // the regex matcher being joined to the pattern
    Matcher regexmatcher = checkRegex.matcher(stuffToCheck);
    // define an ArrayList
    ArrayList<String> result = new ArrayList<>();

    int end = stuffToCheck.length();
    for (int i = 1; i <= end; i++) {

        for (int j = 0; j <= end-i; j++) {

            regexmatcher.region(j, j + i);
            while (regexmatcher.find()) {
                if (result.indexOf(regexmatcher.group()) == -1) {         

                    System.out.println(regexmatcher.group());
                    //result.add(regexmatcher.group());
                }
            }    
        }
    }
}

我发现最简单的方法是先抓取最长的匹配,然后在相同的起始位置连续进行较短的匹配。外循环中的
find()
定位下一个匹配,然后内循环将区域设置为其边界并稳步缩小。我使用了
lookingAt()
在内部循环中,因为它会自动将匹配锚定到区域的开头;可能不需要,但无论如何,它就在那里。:D

public static void regexchecker(String regex, String source)
{
  Pattern p = Pattern.compile(regex);
  Matcher m = p.matcher(source);
  int len = source.length();
  int start = 0;
  int end = len;

  while (start < len && m.region(start, len).find())
  {
    start = m.start();
    end = m.end();
    while (start < end && m.region(start, end).lookingAt())
    {
      System.out.println(m.group());
      end = m.end() - 1;
    }
    start++;
  }
}

我发现最简单的方法是先抓取最长的匹配,然后在相同的起始位置连续进行较短的匹配。外循环中的
find()
定位下一个匹配,然后内循环将区域设置为其边界并稳步缩小。我使用了
lookingAt()
在内部循环中,因为它会自动将匹配锚定到区域的开头;可能不需要,但无论如何,它就在那里。:D

public static void regexchecker(String regex, String source)
{
  Pattern p = Pattern.compile(regex);
  Matcher m = p.matcher(source);
  int len = source.length();
  int start = 0;
  int end = len;

  while (start < len && m.region(start, len).find())
  {
    start = m.start();
    end = m.end();
    while (start < end && m.region(start, end).lookingAt())
    {
      System.out.println(m.group());
      end = m.end() - 1;
    }
    start++;
  }
}

你为什么要使用正则表达式呢?这是一个a然后AB然后ABC的序列,而不是AX吗?根据你的需求描述和你构建代码的方式,我想说你想要的输出是
a,AB,ABC,B,BC,C,a,AB,ABC,B,BC,C
@GUIDO是的,我已经相应地编辑了我的帖子为什么要使用正则表达式呢?这是一个序列吗然后是AB,然后是ABC,但不是AX?鉴于您的需求描述和您构建代码的方式,我想说您想要的输出将是
A、AB、ABC、B、BC、C、A、AB、ABC、B、BC、C
@GUIDO是的,我已经相应地编辑了我的帖子