我想使用正则表达式使用单个匹配来解析项目。 我需要将项目分别分配给组。
custPowerCourse := 11.1,22.2,33.3,44.4,55.5;
这是建议的RegEx
((\w+)\s)?:=(\s?("[\w\s]*"|(\d+\.\d+)*)\s?(,|;|$))+
答案 0 :(得分:0)
答案 1 :(得分:0)
如何将结果存储在列表中,然后从中读取?
public static void main(String[] args) {
final String txt = "11.1,22.2,33.3,44.4,55.5;";
final String re1="([+-]?\\d*\\.\\d+)(?![-+0-9\\.])"; //regex to match floats
final Pattern p = Pattern.compile(re1, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
final Matcher m = p.matcher(txt);
final List<Float> results = new ArrayList<Float>();
while (m.find()) {
final String float1 = m.group(0);
results.add(Float.parseFloat(float1));
}
for(final Float f : results){
System.out.println(f);
}
}
答案 2 :(得分:0)
有时当正则表达式开始变得复杂时,我喜欢一步一步地进行(这次是在perl中):
#!/usr/bin/perl
use Data::Dumper;
my $a= "custPowerCourse = 11.1,22.2,33.3,44.4,55.5;";
if( ($lhs,$rhs)= $a =~ /(\w+)\s*:=\s*(.*);/){ ## if a is lhs := rhs,
@g = $rhs =~ /([\d.]+)/g; ## extract numbers from rhs
print Dumper(\@g);
}