java - 使用正则表达式 Java 获取重叠模式

标签 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, 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());
                    }
                }    
            }
        }
    }
}

好的,所以我知道我的代码每次都会从 j 迭代到结束,但我需要它跳过给出相同输出的迭代。

我的输出是

  1. A
  2. AB
  3. ABC
  4. ABC
  5. ABC a
  6. ABC ab
  7. ABC abc

等等,当我想要这样的输出时

  1. A
  2. B
  3. C
  4. a
  5. b
  6. c
  7. AB
  8. BC
  9. ab
  10. bc
  11. ABC
  12. abc

非常感谢任何帮助。我的原始数据集比这大得多,但为简单起见,我使用了 7 个字符集

最佳答案

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

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

关于java - 使用正则表达式 Java 获取重叠模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36522049/

相关文章:

php - 如何在 PHP 中提取 <tr> 标记的内容?

c++ - std::regex 的正则表达式选项

Javascript .replace()/正则表达式,代码为字符串

algorithm - 查找二维 map 中无法到达的部分

algorithm - 如何充分计算这个矩阵

java - Spring Data JPA 存储库 : Is it possible to give the params a default Value/default all?

java - 当我做任何事情时 JNI 崩溃 :S

java - 如何在Eclipse中配置外部工具来运行jar文件并加载其他类文件?

c++ - 快速等效于 STK 中引用的 DSP 的 sin()

java - 适用于 Android 的 Firebase 数据库,检索数据时遇到问题