试试这个模式“ \\b([GCATgcat]+)\\b
”,它匹配由单词边界包围的任何 GCAT 字符序列(大写或小写)(因此它不会匹配嵌入在其他字符串中的那些字符,例如单词“catalog”)。如果您在示例文件中反复扫描此正则表达式,您将提取每个序列。
这是您的示例文件的一个工作示例:
// Locate the substring between "ORIGIN" and "//" in the file.
String fileContents = getSampleFileContents();
int indexOfOrigin = fileContents.indexOf("ORIGIN");
String pertinentSection = fileContents.substring(
indexOfOrigin, fileContents.indexOf("//", indexOfOrigin));
// Search for sequences within the pertinent substring.
Pattern p = Pattern.compile("\\b([GCATgcat]+)\\b");
Matcher m = p.matcher(pertinentSection);
List<String> sequences = new ArrayList<String>();
while (m.find()) {
sequences.add(m.group(1));
}
sequences.toString(); // => ["acagatgaag", "acagatgaag", ..., "acagatgaag"]