lundi 23 janvier 2017

Setting up regex in Pattern object for java string

Am trying to get all matches in a java string. The matches must be bases and powers in a math equation. As you know, bases and powers could be negative and decimals as well. I have the pattern, regex and matcher set up. It looks something like this, but it is not giving me what I expect. I guided myself by this post here on StackOverflow Regex to find integer or decimal from a string in java in a single group?

Am really just interested in capturing powers that have negative exponents both integers and non-integers.

Well here is my code:

String ss = "2.5(4x+3)-2.548^-3.654=-14^-2.545";
String regex = "(\\d^+(?:\\d+)?)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(ss);

while(m.find()){
        System.out.println("Wiwi the cat: "+m.group(1));
    }

The output of this code is nothing. Any ideas or suggestions would be great.

thanks

Aucun commentaire:

Enregistrer un commentaire