此代码用于从数据集中提取顺序字母
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
迭代到结束,但我需要它跳过给出相同输出的迭代。
我的输出是
- A
- AB
- ABC
- ABC
- ABC a
- ABC ab
- ABC abc
等等,当我想要这样的输出时
- A
- B
- C
- a
- b
- c
- AB
- BC
- ab
- bc
- ABC
- abc
非常感谢任何帮助。我的原始数据集比这大得多,但为简单起见,我使用了 7 个字符集
最佳答案
由于您正在设置要在您的区域中检查的确切边界,因此您想要排除仅匹配该区域的一部分的匹配项,因为它们将在不同的迭代中找到。由于默认情况下 Matcher 在设置区域时将 anchor 边界应用到该区域,因此在正则表达式中使用 anchor 来消除重复结果:
regexchecker ("^\\D+$", a);
关于java - 使用正则表达式 Java 获取重叠模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36522049/