String a = \"sin(23)+cos(4)+2!+3!+44!\";
a.replaceAll(\"\\D\"); //Not working it is only extracting Digits
I want to extract the numbers which are wit
First thing: Strings are immutable. You code you tried should be more like
a = a.replaceAll("\\D","");
Second, if you are sure that you will not have more complext expressions like ((1+2)!+3)!
then you can use appendReplacement
and appendTail
methods from Matcher class.
String a = "sin(23)+cos(4)+2!+3!+44!";
StringBuffer sb = new StringBuffer();
Pattern p = Pattern.compile("(\\d+)!");
Matcher m = p.matcher(a);
while(m.find()){
String number = m.group(1);//only part in parenthesis, without "!"
m.appendReplacement(sb, calculatePower(m.group(number )));
}
m.appendTail(sb);
a = sb.toString();