Java正则表达式匹配器始终返回false

Java正则表达式匹配器始终返回false,java,regex,matcher,Java,Regex,Matcher,我有一个字符串表达式,需要从中获取一些值。字符串如下所示 #min({(((fields['example6'].value + fields['example5'].value) * ((fields['example1'].value*5)+fields['example2'].value+fields['example3'].value-fields['example4'].value)) * 0.15),15,9.087}) 从这个stribg中,我需要获得一个字符串数组列表,其中包含

我有一个字符串表达式,需要从中获取一些值。字符串如下所示

#min({(((fields['example6'].value + fields['example5'].value) * ((fields['example1'].value*5)+fields['example2'].value+fields['example3'].value-fields['example4'].value)) * 0.15),15,9.087})
从这个stribg中,我需要获得一个字符串数组列表,其中包含诸如“example1”、“example2”等值

我有一个Java方法,如下所示:

String regex = "/fields\\[['\"]([\\w\\s]+)['\"]\\]/g";
ArrayList<String> arL = new ArrayList<String>();
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(expression);

while(m.find()){
    arL.add(m.group());
}
String regex=“/fields\\['\\”]([\\w\\s]+)['\“]\\]/g”;
ArrayList arL=新的ArrayList();
Pattern p=Pattern.compile(regex);
Matcher m=p.Matcher(表达式);
while(m.find()){
arL.add(m.group());
}

但是
m.find()
总是返回
false
。有什么我遗漏的吗?

您似乎遇到的主要问题是,您使用的分隔符(如PHP、Perl或JavaScript)不能在Java正则表达式中使用。此外,您的匹配项位于第一个捕获组中,但您使用的是返回整个匹配项的
group()
(包括
字段['

以下是工作代码:

String str = "#min({(((fields['example6'].value + fields['example5'].value) * ((fields['example1'].value*5)+fields['example2'].value+fields['example3'].value-fields['example4'].value)) * 0.15),15,9.087})";
ArrayList<String> arL = new ArrayList<String>();
String rx = "(?<=fields\\[['\"])[\\w\\s]*(?=['\"]\\])";
Pattern ptrn = Pattern.compile(rx);
Matcher m = ptrn.matcher(str);
while (m.find()) {
    arL.add(m.group());
}
String str=“#min({(((字段['example6'].value+字段['example5'].value)*)((字段['example1'].value*5)+字段['example2'].value+字段['example3'].value字段['example4'].value))*0.15,9.087});
ArrayList arL=新的ArrayList();

String rx=“(?问题出在“/”上。如果您只想提取字段名,则应使用m.group(1):

String regex=“fields\\\['\\”]([\\w\\s]+)['\“]\\]”;
ArrayList arL=新的ArrayList();
Pattern p=Pattern.compile(regex);
Matcher m=p.Matcher(表达式);
while(m.find()){
arL.add(m.group(1));
}

在Java正则表达式中不使用
/…/g
语法。它们是其他语言中的正则表达式分隔符,这在Java中是不必要的。删除它们,正则表达式将正常工作。
String regex = "fields\\[['\"]([\\w\\s]+)['\"]\\]";
ArrayList<String> arL = new ArrayList<String>();
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(expression);

while(m.find()){
    arL.add(m.group(1));
}