Reputation: 752
I need to find a pattern in a Java String and replace for another string.
I have two kinds of Strings
1- Lorem ipsum dolor sit amet, it. Integer pos laciniA. 2. Lorem ipsum dolorelit. Curabitur pretium maL.
What I need to do is to find a "." that don't have a number before it and replace by ";"
I've found the regex to do this like str.replaceAll('\.', '\\;');
The problem with this patter is that when it find the String "2." it changes by "2;"
Another solution is str.replaceAll('[a-zAz]\.', '\\;'); but it replace the last letter before ".".
Someone can help-me with this?
Upvotes: 1
Views: 50
Reputation: 626845
What I need to do is to find a "." that don't have a number before it and replace by ";"
You may leverage a negative lookbehind:
String result = str.replaceAll("(?<!\\d)\\.", ";");
See the regex demo
Details:
(?<!\d)
- fails the match if there is a digit before the current location\.
- matches a literal .
Upvotes: 1