如何以这种格式编写java模式:任意字符(int,int)(int,int)number任意数量(int,int,int)
比如说如何以这种格式编写java模式:任意字符(int,int)(int,int)number任意数量(int,int,int),java,regex,Java,Regex,比如说 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 = Pat
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));
}
}
它无法获取最后一个元组(0,0,0)的值。好的,对不起,我必须修改。java matcher似乎不喜欢在正则表达式编译时无法确定的模式计数。但这是可行的(经过测试):
Matcher m=Pattern.compile(\\(\\d+),(\\d+),(\\d+),(\\d+).Matcher((23,56,78)(54,22,11));
while(m.find())
{
对于(inti=1;i我不知道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
}
匹配位图记录时未完成
("
([A-z][^\s])maze.bmp'~第1组
\+
\(\d+,(\d+)\“0”“0”~组2,3
\+
\(\d+,(\d+)\'319''239'~组4,5
\+
(\d+“65”~第6组
\+
(\d+“120”~第7组
\+
(
(?:\(\d+,\d+,\d+\)\s+'(254243,90)(0,0,0)')~8组
)
“”-context=global
{
//保存到BITMAP.array(组1-7)
将组8复制到变量“(254243,90)(0,0,0)”中
元组的新匹配,组8是此新匹配的正则表达式主题
("
(\d+)
“”-context=global
追加TUPLES.array(254 243 90 0)
到BITMAP.array(maze.bmp 0 0 319 239 65 120 254 243 90 0 0)
//做下一个位图记录
}
这里是我能想到的最好的方法。请注意,我使用了两种模式,因为出于某种原因,Java拒绝捕获重复组(如果有人知道原因,请留下评论)
谢谢。为什么会有?和:普通括号同时创建一个子模式和一个组。我想要子模式(这样我就可以在上面使用量词),但我不在乎把它变成一个组。所以我在开始处添加?:这会抑制组部分。我使用Matcher匹配输入,并尝试使用组来获取数字(int index)函数,但我只能获取最后一个元组的数目。例如,如果我的输入是(23,56,78)(54,22,11)我只能得到54 22和11。我不知道如何解决这个问题。这可能是因为我忘记了组中的空格。等等,我编辑…仍然只能得到最后一个元组。谢谢你,你是说你只有元组吗?如果是,你可以在(\\d+)上进行全局匹配不,请重新阅读问题。我刚刚编辑过。stackoverflow将\\更改为\谢谢您知道[A-z]
通常匹配非字母字符(例如,[
,^
和
)还有?在Z
和a
之间有一个间隙。谢谢你指出。我会改变它的。谢谢。我不明白。你不明白到底是什么?问扬基,他明白了,看看他的代码。。Java拒绝捕获重复组的某些原因
这与Java无关。正则表达式Session捕获组保留它捕获的最后一项。因此,'aabbb'=~/(a)+(b)+/
分别捕获a和b。@sin:在PCRE中,您也可以获取所有重复的组。这是PCRE的非标准特性吗?(您能否引用RFC或类似的语句,说明java所做的是预期的标准行为?)谢谢。@yankee-这可能是对分组行为的误解。只有一个捕获组可以被反向引用,因此,它在任何给定时间的内容都必须是可引用的。一个组将包含它封装的原子表达式的值,并且永远不会超过它的右括号。因此'00aaabb00'=~/((a)+(b)+/
第1组='aaabbb',第2组='a',第3组='b'。听起来不错吗?@sin:是的,我理解设计。但是使用PCRE你可以做这样的事情:它工作得非常好,可以在这里用一种模式解析文本。我理解你描述的概念,但在某些情况下,另一个概念也很好(比如这一个)。我想知道其中一个概念是否有某种标准(或两者都有)。或者是否可以在Java中设置一个标志,以获得与使用PCRE的链接脚本中相同的行为。@yankee不,你误解了。@array='a-b-c-a-b-c-c a-b-c'=~(a)-(b)-(c)/g;@array现在等于(a,b,c,b,c)…所有匹配的捕获缓冲区都累积到数组中,与此无关。您不了解的是锚点的可重复性,在本例中,a-b-c是一个完整的匹配,一条记录。您不能只在记录中执行a+,然后期望获得“aaa”…顺便说一句,PCRE基于Perl 5.8
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));
}
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
}
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);