java - Regular Expressions - match a string containing "+" and "-" -


i have string, 1+++-3--+++++2 includes + , - sign. want represent + , - part + or - sign. if there odd number of - sign in string, replace -, , + if number. how can using regex?

for example, have math expression, 1+-+-2-+--+3. replaced 1+2-3

you can create array of operators , use for loop count occurrences of 1 character. example:

string expression = "1+++-3--+++++2"; string[] str = expression.split("[0-9]+");  for(op : str) {     int count = 0;     for(int =0; < str.length(); i++)         if(op.charat(i) == '-')             count++;      if(count % 2 == 0) {         op = "-";     }     else {         op = "+";     } } 

after assigning modified one-character operators in str[], should relatively simple write new expression.


Comments