I have this String:
foo bar 567 baz
Now I want to add before each number the String num:.
So the result has to be:
foo bar num:567 baz
Also this has to work:
foo 73761 barbazboom!! 87
result:
foo num:73761 barbazboom!! num:87
The regex to search number is this: [0-9]+
But I want to replace the matching substring with num: + [the matching substring]
I now wrote an example with numbers, but an other example can be: add before each e-mail-address Email found:
Cœur
39k25 gold badges207 silver badges282 bronze badges
asked Dec 8, 2009 at 11:58
Martijn Courteaux
69.1k48 gold badges202 silver badges297 bronze badges
2 Answers 2
Make use of grouping. You can use the parentheses ( and ) to define groups and identify the group in the result by $n where n is the group index.
String string = "foo bar 567 baz";
String replaced = string.replaceAll("(\\d+)", "num:1ドル");
answered Dec 8, 2009 at 12:02
BalusC
1.1m377 gold badges3.7k silver badges3.6k bronze badges
Sign up to request clarification or add additional context in comments.
Comments
"foo bar 567 baz".replaceAll("(\\d+)", "num:1ドル");
answered Dec 8, 2009 at 12:01
dfa
117k32 gold badges193 silver badges229 bronze badges
Comments
lang-java
num:[0-9]+case?