0

I would like to parse the items with a single matching using a regular expression. I need the items to be assigned to groups individually.

custPowerCourse := 11.1,22.2,33.3,44.4,55.5;

Here is a proposed RegEx

((\w+)\s)?:=(\s?("[\w\s]*"|(\d+\.\d+)*)\s?(,|;|$))+

3 Answers3

0
(\d+(?:\.\d+))|(\w+)

Try this.Grab the captures.See demo.

http://regex101.com/r/hQ9xT1/25

vks
  • 67,027
  • 10
  • 91
  • 124
0

How about storing the results in a list, then reading from it?

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);
    }
}
MihaiC
  • 1,618
  • 1
  • 10
  • 14
0

Sometimes when regex start to became complex, I like to do it step by step (this time in 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);
}
JJoao
  • 4,891
  • 1
  • 18
  • 20