how to write a java pattern in this format: any characters (int,int) (int,int) number number any number of (int,int,int)
For example
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)
I want to get the maze0.bmp and all the numbers. I have:
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));
}
}
but it's correct. For example, if my input is
Maze0.bmp (0,0) (319,239) 65 120 (254,243,90) (0,0,0)
it cannot get the the last tuple( 0,0,0).
Here is the best I can come up with. Note, that I used TWO patterns, because for some reason Java refuses to capture repeating groups (if anyone happens to know why, plz leave a comment).
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);
Ok, sorry, I have got to revise. The java matcher seems to not like pattern counts it can't determine at compile time of the regex. But this works (tested):
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));
}
I don't know the context of matching in java, but I know regex very well. Try this context:
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
}
精彩评论