[英]Java Regex String Matching
我正在使用正則表達式匹配器進行近似字符串匹配。 我有一個關於如何使其允許重疊匹配的問題。 當前,它找到一個匹配項,然后跳到該匹配項的末尾並從此處開始搜索。
當前代碼
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())+"|......");
}
}
}
您可以使用public boolean find(int start)
並遍歷整個String
int index = 0;
while (index < nucleotides.length() && m.find(index)) {
//your code here
index=m.start()+1;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.