您可以将此模式与全球研究结合使用:
(?m:^U\d+:|\G/)%x\[(-?\d+),(-?\d+)\]
图案细节:
(?m: # non capturing group with the multiline modifier
^ # anchor: start of the line
U\d+: # literal "U" followed by digits and :
| # OR
\G/ # literal "/" contiguous to a precedent match
)
%x\[(-?\d+),(-?\d+)\]
例:
Pattern p = Pattern.compile("(?m:^U\\d+:|\\G/)%x\\[(-?\\d+),(-?\\d+)\\]");
Matcher m = p.matcher(s); // s is all the content of your txt file
while (m.find()) {
System.out.print(m.group(1) + "," + m.group(2));
}
如果允许两个%x [a,b]之间的其他内容,您可以将模式更改为:
(?m:^U\d+:|\G(?>[^#\n/]++|/(?!%x\[))*/)%x\[(-?\d+),(-?\d+)\]
要么
(?m:^U\d+:|\G[^#\n]*?/)%x\[(-?\d+),(-?\d+)\]
请注意,添加的子模式与注释不匹配,因为字符#是从字符类中排除的
另一种方式:由于您的数据位于文本文件中,您可以逐行读取文件并使用其中一种先前模式提取数字(在这种情况下,您可以删除m修饰符).优点是你知道数字来自哪一行.