Click here to Skip to main content
15,178,008 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
replace//

number(number)
//to
number*(number)


//or


(number)number
//to
(number)*number

//or

(Number)(number)
//To
(Number)*(number)

//etc
//Java

What I have tried:

I tried it manually like this

userExp = userExp.replace(")0" , ")*0");
userExp = userExp.replace(")1" , ")*1");
userExp = userExp.replace(")2" , ")*2");
userExp = userExp.replace(")3" , ")*3");
userExp = userExp.replace(")4" , ")*4");
userExp = userExp.replace(")5" , ")*5");
userExp = userExp.replace(")6" , ")*6");
userExp = userExp.replace(")7" , ")*7");
userExp = userExp.replace(")8" , ")*8");
userExp = userExp.replace(")9" , ")*9");
userExp = userExp.replace(")π" , ")*π");

But I want to simplify it to like a single line of two.
Posted
Updated 26-Nov-21 21:51pm

1 solution

Use a regex: Java Regular Expressions[^]
\)(\d)
Should find it, and
)*$1
should replace it.
   
Comments
1B_Vincent Renzo Quilon 27-Nov-21 12:12pm
   
can You explain that to me?
1B_Vincent Renzo Quilon 27-Nov-21 12:12pm
   
I'm not familiar with regex
1B_Vincent Renzo Quilon 27-Nov-21 12:17pm
   
How to apply that to this?
userExp = userExp.replace(); ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900