-3

所有这些条件的单个 正则表达式 1. 应该只允许一个字母数字 2. 单词之间只允许一个空格 3. 应该只允许特殊字符,如 -.,' 4. 不应该允许前导空格、尾随空格和连续空格。

有效输入:

"testing with 2 regx solution"

输入无效:

" testing    with 2 regx solution" or "testing  %^with 2 regx solution "
4

2 回答 2

6

尝试这个

^(\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
于 2012-12-06T14:52:50.127 回答
-1
 inputString= Regex.Replace(inputString.Trim(),@"\s+"," ");

--SJ

于 2012-12-06T14:28:16.547 回答