Reputation: 363
in java i need to replace a number with a word only if it is not preceeded by the "+". Example:
- match1
- match+1
Should become:
matchone
match+1 (no modify)
I tried with
>>>name = name.replaceAll("([^+])1", "one");
matcone //required "matchone"
But it is not working. Any suggestions?
Thank you
Upvotes: 0
Views: 207
Reputation: 31299
Your regex is eating the character before the one and replacing that with "one" as well, so the output in the first instance is "matcone".
You can use a negative look-behind expression (?<!
) to match any "1" that is not preceded by a "+":
name = name.replaceAll("(?<!\\+)1", "one");
Upvotes: 1
Reputation: 91518
Use a negative lookbehind:
name = name.replaceAll("(?<!\\+)1", "one");
Upvotes: 6