gpt4 book ai didi

java - 如何以这种格式编写java模式 : any characters (int, int) (int,int) number number any number of (int,int,int)

转载 作者:行者123 更新时间:2023-12-01 15:56:44 30 4
gpt4 key购买 nike

例如

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)。

最佳答案

这是我能想到的最好的。请注意,我使用了两种模式,因为出于某种原因 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);

关于java - 如何以这种格式编写java模式 : any characters (int, int) (int,int) number number any number of (int,int,int),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4911671/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com