例如
Maze0.bmp (0,0) (319,239) 65 120
Maze0.bmp (0,0) (319,239) 65 120 (254,243,90)
Maze0.bmp (0,0) (319,239) 65 120 (254,243,90) (0,0,0)
Maze0.bmp (0,0) (319,239) 65 120 (254,243,90) (0,0,0) (11,33,44)
我想获得maze0.bmp和所有数字。我有:
Pattern pattern = Pattern.compile("([A-z][^\\s]*)\\s+\\((\\d+),(\\d+)\\)\\s+\\((\\d+),(\\d+)\\)\\s+(\\d+)\\s+(\\d+)\\s+(\\((\\d+),(\\d+),(\\d+)\\)\\s*)");
BufferedReader stdin = new BufferedReader(new InputStreamReader( System.in));
String input;
Matcher matcher = null;
boolean isMatched = false;
while (!isMatched) {
System.out.println("Please enter right format\n");
input = stdin.readLine();
matcher = pattern.matcher(input);
while(matcher.find()) {
isMatched = true;
for (int i = 1; i <= matcher.groupCount(); ++i)
System.out.println(matcher.group(i));
}
}
但这是正确的。例如,如果我的输入是
Maze0.bmp (0,0) (319,239) 65 120 (254,243,90) (0,0,0)
它无法获取最后一个元组(0,0,0)。
答案 0 :(得分:1)
这是我能想到的最好的。请注意,我使用了两种模式,因为出于某种原因,Java拒绝捕获重复组(如果有人碰巧知道原因,请发表评论)。
final Pattern outerPattern = Pattern.compile("(.*?) \\((\\d+),(\\d+)\\) \\((\\d+),(\\d+)\\) (\\d+) (\\d+)(.*)");
final Pattern optionalTouplePattern = Pattern.compile(" \\((\\d+),(\\d+),(\\d+)\\)");
final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
boolean isMatched;
do
{
System.out.println("Please enter right format:");
Matcher m = outerPattern.matcher(stdin.readLine());
if (isMatched = m.find())
{
System.out.println(String.format("name='%s', first touple: [%s,%s], second touple: [%s,%s], first single number: %s, second single number: %s", m.group(1), m.group(2), m.group(3), m.group(4), m.group(5), m.group(6), m.group(7)));
m = optionalTouplePattern.matcher(m.group(8));
while(m.find())
{
System.out.println(String.format("+ optional touple: [%s,%s,%s]", m.group(1), m.group(2), m.group(3)));
}
}
}while(!isMatched);
答案 1 :(得分:0)
好的,对不起,我得修改一下。 java匹配器似乎不喜欢它在正则表达式的编译时无法确定的模式计数。但这有效(经过测试):
Matcher m = Pattern.compile("\\((\\d+),(\\d+),(\\d+)\\)").matcher("(23,56,78) (54,22,11)");
while(m.find())
{
for(int i = 1; i <= m.groupCount(); ++i)
System.out.println(m.group(i));
}
答案 2 :(得分:0)
我不知道java中匹配的上下文,但我非常了解正则表达式。 试试这个背景:
while matching BITMAP records is not done
("
([A-z][^\s]) 'maze.bmp' ~ group 1
\s+
\( (\d+),(\d+) \) '0' '0' ~ group 2,3
\s+
\( (\d+),(\d+) \) '319' '239' ~ group 4,5
\s+
(\d+) '65' ~ group 6
\s+
(\d+) '120' ~ group 7
\s+
(
(?: \( \d+,\d+,\d+ \) \s+ )+ '(254,243,90) (0,0,0) ' ~ group 8
)
") - context = global
{
// save to BITMAP.array (groups 1 - 7)
copy group 8 to variable '(254,243,90) (0,0,0) '
new matching of TUPLES, group 8 is the regex subject for this new match
("
(\d+)
") - context = global
append TUPLES.array (254 243 90 0 0 0)
to BITMAP.array (maze.bmp 0 0 319 239 65 120 <append> 254 243 90 0 0 0)
// do next BITMAP record
}