我不知道你的意思到底是什么,但我猜你要检查恰好一个空白,但任意数量的非空白字符:
@"^\S*\s\S*$"
Run Code Online (Sandbox Code Playgroud)
示例代码:
Regex regex = new Regex(@"^\S*\s\S*$");
Console.WriteLine(regex.IsMatch("Hello, world!"));
Console.WriteLine(regex.IsMatch("This contains three spaces."));
Console.WriteLine(regex.IsMatch("Two\nlines."));
Run Code Online (Sandbox Code Playgroud)
输出:
True False True
其他变化
要检查字符串是否只包含一个空格(没有其他字符):
@"^\s$"
Run Code Online (Sandbox Code Playgroud)
要检查字符串是否至少包含一个空格:
@"\s"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
392 次 |
| 最近记录: |