Java: Find Integers in a String (Calculator) -
if have string
looks this: string calc = "5+3"
. can substring integers
5
, 3
?
in case, know how string looks, this: string calc = "55-23"
therefore, want know if there way identify integers in string.
for that, regular expression friend:
string text = "string calc = 55-23"; matcher m = pattern.compile("\\d+").matcher(text); while (m.find()) system.out.println(m.group());
output
55 23
now, might need expand support decimals:
string text = "string calc = 1.1 + 22 * 333 / (4444 - 55555)"; matcher m = pattern.compile("\\d+(?:.\\d+)?").matcher(text); while (m.find()) system.out.println(m.group());
output
1.1 22 333 4444 55555
Comments
Post a Comment