regex将匹配除空格之外的单个字符

时间:2021-03-22 20:11:39

I need to match a single character that is anything but a space but I don't know how to do that with regex.

我需要匹配一个除了空格之外的字符,但是我不知道如何使用regex来实现这一点。

2 个解决方案

#1


173  

The following should suffice:

以下应该足够了:

[^ ]

If you want to expand that to anything but white-space (line breaks, tabs, spaces, hard spaces):

如果你想把它扩展到除空白外的任何地方(换行符、制表符、空格、硬空格):

[^\s]

or

\S

#2


93  

  • \s matches any white-space character
  • \s匹配任何空格字符
  • \S matches any non-white-space character
  • \S匹配任何非空白字符
  • You can match a space character with just the space character;
  • 可以将空格字符与空格字符匹配;
  • [^ ] matches anything but a space character.
  • [^]匹配一个空格字符。

Pick whichever is most appropriate.

挑选最合适的。

#1


173  

The following should suffice:

以下应该足够了:

[^ ]

If you want to expand that to anything but white-space (line breaks, tabs, spaces, hard spaces):

如果你想把它扩展到除空白外的任何地方(换行符、制表符、空格、硬空格):

[^\s]

or

\S

#2


93  

  • \s matches any white-space character
  • \s匹配任何空格字符
  • \S matches any non-white-space character
  • \S匹配任何非空白字符
  • You can match a space character with just the space character;
  • 可以将空格字符与空格字符匹配;
  • [^ ] matches anything but a space character.
  • [^]匹配一个空格字符。

Pick whichever is most appropriate.

挑选最合适的。