How to replace the blank
Given the code:
import cl开发者_StackOverflow中文版r
clr.AddReference('System')
from System.Text.RegularExpressions import *
def ReplaceBlank(st):
return Regex.Replace(
st,r'[^a-z\s](\s+)[^a-z\s]',
lambda s:s.Value.Replace(' ', ''),RegexOptions.IgnoreCase)
I expect the input ABC EDF
to return ABCDEF
but it doesn't work, what did I do wrong?
[^a-z\s]
with ignore-case flag set matches anything other than letters and whitespace characters. ^
at the beginning of a character class (the thing between []
) negates the character class.
To replace blanks, you can simply replace \s+
with empty strings or, if you need to match only letters replace
(?<=[a-z])\s+(?=[a-z])
with an empty sting. The second regex will match string of whitespaces between two letters; to account for beginning/end of strings, use
(?<=(^|[a-z]))\s+(?=($|[a-z]))
or
\b\s+\b
The second one will match spaces between two word boundaries, which include symbol chars like period, comma, hyphen etc.
精彩评论