在java正则表达式中捕获同一组的多个实例

在java正则表达式中捕获同一组的多个实例,java,regex,capture-group,Java,Regex,Capture Group,我正在尝试使用正则表达式从pascal代码字符串中提取参数名,这是我尝试处理的最复杂的问题。注意,永远不会有空白,括号将始终存在 (rate:real;interest,principal:real) 我目前得到的回复如下: [(](?:([\w]*)(?:[:][\w])?[;|,]?)*[)] 我希望在参数重新传递时可以访问每个捕获组,但显然不行。对于上面的例子,我需要的值是“利率”、“利率”和“本金” 有解决办法吗?我自己的努力让我想到了使用 “匹配器()与while…find() 我

我正在尝试使用正则表达式从pascal代码字符串中提取参数名,这是我尝试处理的最复杂的问题。注意,永远不会有空白,括号将始终存在

(rate:real;interest,principal:real)
我目前得到的回复如下:

[(](?:([\w]*)(?:[:][\w])?[;|,]?)*[)]
我希望在参数重新传递时可以访问每个捕获组,但显然不行。对于上面的例子,我需要的值是“利率”、“利率”和“本金”

有解决办法吗?我自己的努力让我想到了使用

“匹配器()与while…find()


我不完全理解正则表达式,如果有任何帮助,我将不胜感激。谢谢。

这里有一种使用相对简单的正则表达式的方法:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest {

    public static void main(String[] args) {
        String simple = "(rate:real;interest,principal:real)";
        String regex = "(\\w+:|\\w+,)";

        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(simple);

        while (m.find()) {
            System.out.println(m.group().substring(0, m.group().length() - 1));
        }
    }
}
恐怕我不懂帕斯卡语,但你后面的名字似乎以冒号或逗号结尾。正则表达式查找这些字符串,然后删除最后一个字符(冒号或逗号)

我从测试运行中获得的输出是:

rate
interest
principal

对此,您可以使用
正向查找

((?<=[\(,;])[A-Za-z_]\w*)

)谢谢!这正是我所需要的。
(
  (?<=   #Positive look behind
    [\(,;] #Finds all position that have bracket, comma and semicolon
  )   
  [A-Za-z_]\w* #After finding the positions, match all the allowed characters in variable name following that position
)
String line = "(rate:real;interest,principal:real)";
String pattern = "((?<=[\\(,;])[A-Za-z_]\\w*)";

Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);

while (m.find()) {
    System.out.println(m.group(1));
}