Java replace all text

String.replaceAll without RegEx

I’d like to replace all instances of a substring in a string but String.replaceAll() only accepts a pattern. The string that I have came from a previous match. Is it possible to add escapes to the pattern that I have or is there a version of replaceAll() in another class which accepts a literal string instead of a pattern?

2 Answers 2

NB: replace doesn’t just replace the first occurrence, it replaces all occurrences, like replaceAll .

System.out.println(«hello world, hello life, hello you».replace(«hello»,»hi»)); returns «hi world, hi life, hi you» .

I believe the naming of replaceAll (regex method) was to differentiate from replaceFirst (regex method), not so much to differentiate from replace (non regex method).

The method to add escapes is Pattern.quote() .

String replaced = myString.replaceAll(Pattern.quote(matchingStr), replacementStr) 

But as Jon says you can just use replace() . Despite the fact that it deviates from the replaceAll name, it does replace all occurrences just like replaceAll() .

@PavloZvarych: Pattern.compile() compiles the string as a regular expression, meaning the special characters will be given the special meaning. That’s the complete opposite of what Pattern.quote() does, and what the OP was asking for ( quote() says, «treat the string as a literal»). Maybe you could expand on what «undesirable results» you’re talking about.

@PavloZvarych: If you’re trying to quote the replacement (and not the search pattern), you want to use Matcher.quoteReplacement(«$aa +») . Pattern.compile() produces a Pattern , not a String , so it’s unclear to me how you are even using it in replaceAll .

Источник

Replace all words in java

I’m using the java replaceAll() method for replace matching words in a string. In my case If this word is next to a comma (,) fullstop (.) or anything else like that, this word is not replacing. example : and. and, and; and( This is the code:

body = body.replaceAll("(?i) "+knownWord + " ", replaceWord); 

Can anyone please suggest me an regular expression which is capable of identifying all the words in this string?

Читайте также:  Using JavaBeans in JSP

2 Answers 2

Regular expression visualization

It finds (and captures) words as long as they are not next to commas or periods. Just add whatever punctuation marks you like to the character-classes, such as [. (] .

Here is the regex escaped for a Java string: «(?

As far as ignoring case, just pass the CASE_INSENSITIVE flag to your Pattern object, such as with

Pattern p = Pattern.compile(theAbovePattern, Pattern.CASE_INSENSITIVE); 

+ 1 for illustrative explanation, but I think @Asanka actually wants to replace words also if they are next to .,

Yeah. I thought he meant opposite: That he couldn’t figure out how to capture words next to punctuation 🙂

@aliteralmind Could you please replace this code with that regular expression you wrote. body = body.replaceAll(«(?i) «+knownWord + » «, replaceWord); Because I’m ignoring the case sensitivity as well.

If you want to match specific knownWord do:

 body = body.replaceAll("(?i)\\b"+knownWord + "\\b", replaceWord); 

I think what you were looking for is the \\b (word boundary) it is used to detect where words start/end, so commas or dots should no longer be a problem then.

More detailed example in response to your comment:

 String body = "I'm going to school. "; String knownWord = "school"; String replaceWord = "shop"; System.out.println(body.replaceAll("(?i)\\b"+knownWord + "\\b", replaceWord)); 

The above will print out the following:

Источник

Оцените статью