How to make regex eol `$` to match ONLY "ending position of a String" absolutely (not matching twice at the end)? [duplicate]

know:

$ Matches the ending position of the string or the position just before a string-ending newline. https://en.wikipedia.org/wiki/Regular_expression

ask:

How can I make $ to match ONLY "ending position of the string", NOT "or the position just before a string-ending newline"?

eg:

If you run

String str = "the first sentence\n"
           + "the second sentence\n"
           + "the third sentence\n";
    
System.out.println(str.replaceAll("$", "--"));

current behavior

Output will be

the first sentence
the second sentence
the third sentence--
--

desire behavior

Output desire to be

the first sentence
the second sentence
the third sentence
--

(the reason I need it)

I have the ans now -- use \z Difference $ vs \z



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation