首页 > 解决方案 > Java 正则表达式字符串匹配

问题描述

我正在使用正则表达式匹配器进行近似字符串匹配。我有一个关于如何使其允许重叠匹配的问题。目前,它找到一个匹配项,然后跳到该匹配项的末尾并从那里开始搜索。

当前代码

import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class BNDM2 {
public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    String nucleotides,pattern;
    System.out.print("Enter sequence:");
    pattern = sc.nextLine(); 
    System.out.print("Enter nucleotides:");
    nucleotides= sc.nextLine(); 

    // first convert the pattern into a proper regex
    // i.e. replacing any N with [ATCG]
    Pattern regex = Pattern.compile(pattern.replaceAll("N", "[ATCG]"));

    // create a Matcher to find everywhere that the pattern matches
    Matcher m = regex.matcher(nucleotides);

    // find all the matches
    while (m.find()) {
        System.out.println("Match found:");
        System.out.println("start:" + m.start());
        System.out.println("end:" + (m.end() - 1)); // minus 1 here because the end of a regex match is always off by 1
        System.out.println();
        System.out.println("|" + nucleotides.substring(m.start(),m.end())+"|......");
    }
}

}

标签: javaregex

解决方案


您可以使用public boolean find(int start)并循环遍历整个字符串

 int index = 0;

 while (index < nucleotides.length()  && m.find(index)) {
          //your code here
          index=m.start()+1;
 }

推荐阅读