Simple Java regex not working

I have this regex that should remove sentence delimiters ( . And ? ):

 sentence = sentence.replaceAll("\\.|\\?$",""); 

It works great, it converts

"I am Java developer." to "I am Java developer"

"Am I a Java developer?" to "Am I a Java developer"

But after deployment, we found that it also replaces any other points in the sentence as

"Hi.Am I a Java developer?" becomes "HiAm I a Java developer"

Why is this happening?

+7
java operators string regex operator-precedence
source share
4 answers

pipe ( | ) has the lowest priority for all statements. So your regex:

 \\.|\\?$ 

regarded as:

 (\\.)|(\\?$) 

which matches the string . anywhere in the string and matches the character ? at the end of the line .

To fix this, you need to group . and ? together like:

 (?:\\.|\\?)$ 

You can also use:

 [.?]$ 

In a character class . and ? processed literally, so you do not need to avoid them.

+14
source share

What you say with "\\.|\\?$" Is a "period" or "a question mark as the last character".

I would recommend "[.?]$" Instead, to avoid confusing shielding (and, of course, an undesirable result).

+8
source share

Your problem is with the low priority of the rotation operator | . Your regular expression means a match:

  • . anywhere or
  • ? at the end of the line.

Use a character class instead:

 "[.?]$" 
+7
source share

You forgot to declare characters ending in a sentence with parentheses:

 sentence = sentence.replaceAll("(\\.|\\?)$",""); 

The best approach is to use [.?]$ , As @Mark Byers suggested.

 sentence = sentence.replaceAll("[.?]$",""); 
+3
source share

All Articles