gpt4 book ai didi

java - 正则表达式打印出里面的所有匹配项?和 , 或 )

转载 作者:太空宇宙 更新时间:2023-11-04 12:36:08 25 4
gpt4 key购买 nike

我有一个保存在二维数组中的字符串集合。该字符串具有喇叭子句的形状,完整的一个字符串可以是 Patient(?x) 以及 hasdoctor(?x,?y) 的形式

如果我写 ?x=alex 和 ?y=john 那么上面的字符串采用的结构

病人(亚历克斯)hasdoctor(亚历克斯,约翰)

现在的问题是,当使用下面的代码时它会找到 ?x,但在 hasdoctor(?x,?y) 中它会跳过 ?y 。

void find_var(String[][] temp)
{
System.out.println(temp.length);
System.out.println(temp[0].length);
for(int i=0;i<temp.length;i++)
for(int j=1;j<temp[0].length-1;j++)
{
String text_to_parse=temp[i][j];
Pattern y = Pattern.compile("[?]\\w[,)]");
Matcher z= y.matcher(text_to_parse);
if(z.find())
{
System.out.println("Found at::"+temp[i][j]);
System.out.println(z.group());

}
else
{
System.out.println("Not found at::"+temp[i][j]);
}
}}

我可以在java中解释我想要的伪代码是

if([?]\\w[,) is found in array[][])
if([?]\\w[,) is already in other_array[]
Then skip;
else
save [?]\\w[,) to other_array[]

最佳答案

不能说我完全理解你想要实现的目标,但我认为问题在于你正在使用

if (z.find()) { /* ... */ }

而不是

while (z.find()) { /* ... */ }

使用if,字符串不会被完全消耗,而是在找到第一个匹配后返回。

关于java - 正则表达式打印出里面的所有匹配项?和 , 或 ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37313116/

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