Using java replaceAll method to replace word in sentences with one regex -


i trying replace "is" "is not" in string there exception should not replace "is" reside in other word.

example

"this ant" --> "this not ant" [correct] "this ant" --> "this not not ant" [incorrect] 

so far, did is

string result = str.replaceall("([^a-za-z0-9])is([^a-za-z0-9])","$1is not$2"); result = result.replaceall("^is([^a-za-z0-9])","is not$1"); result = result.replaceall("([^a-za-z0-9])is$","$1is not"); result = result.replaceall("^is$","is not"); 

but think possible 1 regex can't figure out. possible?

use word boundary (\b):

result = str.replaceall("\\bis\\b", "is not"); 

note: \ should escaped. otherwise matches backspace (u+0008).

see demo.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -