简体   繁体   中英

Regex to match a single character in String

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

For example if I have:

" I have played u with no i and no j o o o o x w x x s"

It should return:

" have played with no and"

I have tried:

\s+\w{1}\s+

But when I use it i get:

" have played with no and no o o x x s"

I am missing something? I think it is related to some sort of "overlapping matches".

Your regex works this way:

find space then one character then another space , and remove it. In this case spaces that surround one character can't be matched in test of another character like in case of

_a_b_c
^^^ -this part matches our pattern so it will be removed leaving 

b_c and now neither `b` or `c` is surrounded with spaces so they will not 
    be removed

To solve this problem just include in match one or more spaces (or start of string) and one character after it like (^|\\s+)\\w .
Also to make sure that after this character is at leas one space (or end of string) but to not include this spaces in match you can use look-ahead mechanism like (?=\\s+|$) .

So in case of Java try

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

and in JavaScript

var replaced = text.replace(/(^|\s+)\w(?=\s+|$)/g,"")

BTW \\w will match any character from [a-zA-Z0-9_] so you can change it to something like [a-zA-Z] if you want only letters.

I'm assuming the language is javascript (please check your tags). The problem I see is your regex includes the spaces, so if you have "aa bc", then it matches with " a ", but then c has no spaces before or after itself.

var text=" a bb c dd e f g tt"
var re=/\s*\b\w\b/g //If you're coding in Java, remove the g: "\\s*\\b\\w\\b"
text.replace(re,"") //" bb dd tt"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM