PILA
PILA

Reputation: 683

replaceAll in groovy no work for long sentence

I have this paragraph(this is just a random text) which I want to remove the last part of the paragraph. I have tried using replaceAll but it only works for shorter sentences, for example, The virus that causes COVID-19

but I want to remove: You can be infected by breathing in the virus if you are within close proximity of someone who has COVID-19, or by touching a contaminated surface and then your eyes, nose or mouth.\n\nMost people who fall sick with COVID-19 will experience mild to moderate symptoms and recover without special treatment.

" The virus that causes COVID-19 is mainly transmitted through droplets generated when an infected person coughs, sneezes, or exhales. These droplets are too heavy to hang in the air, and quickly fall on floors or surfaces.\n\nYou can be infected by breathing in the virus if you are within close proximity of someone who has COVID-19, or by touching a contaminated surface and then your eyes, nose or mouth.\n\nMost people who fall sick with COVID-19 will experience mild to moderate symptoms and recover without special treatment."

  String comment = "The virus that causes COVID-19 is mainly transmitted through droplets generated when an infected person coughs, sneezes, or exhales. These droplets are too heavy to hang in the air, and quickly fall on floors or surfaces.\n\nAlso, SOS stands for Style Or Service and we?re here to help you with all your shopping needs. Feel free to contact us at 1.877.765.3009 or e-mail us at [email protected]. #mysolesociety Have a great day! Feel free to contact us at 855-435-5050 or [email protected] if you have any further questions or concerns. Have a great day!"

//produces the right result.
println(comment.replaceAll("Also, SOS stands", " ")) 
//this does not produce the right result, instead it prints the entire paragraph with this part included.
println(comment.replaceAll("Also, SOS stands for Style Or Service and we?re here to help you with all your shopping needs. Feel free to contact us at 1.877.765.3009 or e-mail us at [email protected]. #mysolesociety Have a great day! Feel free to contact us at 855-435-5050 or [email protected] if you have any further questions or concerns. Have a great day!", " ")) 

Please help with suggestions on how to go about this

Upvotes: 0

Views: 120

Answers (1)

Donat
Donat

Reputation: 4833

The character ? has a special meaning in regular expressions. When you want to include it literally, you have to mask it.

For example by using \\? instead of ?.

Or by surrounding the whole string by \\Q and \\E, which means to quote the string (i. E. "\\Qstring with question marks? This works?\\E".

Another solution would be to use Pattern.quote(pattern) which is safer as it masks everything, including \\Q and \\E.

If you do not need regular expressions for pattern matching, it would be best to use comment.replace(target, replacement). This is faster than using a regex.

Have a look at the javadoc!

Upvotes: 3

Related Questions