Given the code:
import clr
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 replacewith an empty sting. The second regex will match string of whitespaces between two letters; to account for beginning/end of strings, use
or
The second one will match spaces between two word boundaries, which include symbol chars like period, comma, hyphen etc.