java - Regex to match a single character in String -


I want a regex to replace a single character with space (or remove).

For example if I have:

  "I have played none of you with jooooxwxxs"  

return it I have tried:

  "played with no and"  

I have tried:

  \ s + \ W {1} \ s +  

But when I use it I find:

  "No more played with ooxxs " 

Am I missing something? I think it is related to some type of "overlapping matches"

Your regex works like this Does:

find space then one letter then the second location , and remove it in this case a character matching the other around a letter It can not be done in the test of the character like

  _a_b_c ^^^ - this part matches our pattern, so it will be discarded BC and now So `B` or` C` is surrounded by empty space, so they will not be removed  

To solve this problem, only one or more spaces (or the beginning of the string) And like a later version, (^ | \ s +) \ w . To make sure that the lease is on one place (or end of the string) after this character, but do not include this empty space in the match, such as (? = \ S + | $) You can use the system.

In case of Java try,

  string newString = yourString.replaceAll ("(^ | \\ s +) \\ w (= \\ s + | $)? "," ");  

and in JavaScript

  var has been changed to = text.replace (/ (^ | \ s +) \ w (? = \ S  [a-zA-Z0- 9_]      from any character BTW  \ w  Code> so you can change it if you want only letters to something like  [a-zA-Z]  


Comments