所有这些条件的单个 正则表达式 1. 应该只允许一个字母数字 2. 单词之间只允许一个空格 3. 应该只允许特殊字符,如 -.,' 4. 不应该允许前导空格、尾随空格和连续空格。
有效输入:
"testing with 2 regx solution"
输入无效:
" testing with 2 regx solution" or "testing %^with 2 regx solution "
尝试这个
^(\w+\s)*\w+$
^ Start of string ( Start of group \w+ Word of one or more characters \s White space ) End of group * Zero or more of the preeceding group \w+ Word of one or more characters $ End of string
inputString= Regex.Replace(inputString.Trim(),@"\s+"," ");
--SJ