1

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?

1

1 Answer 1

1

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 .
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.