239

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

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ryan
  • 11,743
  • 16
  • 37
  • 37

3 Answers3

334

The following should suffice:

[^ ]

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

[^\s]

or

\S  # Note this is a CAPITAL 'S'!
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
Andrew Moore
  • 93,497
  • 30
  • 163
  • 175
160
  • \s matches any white-space character
  • \S matches any non-white-space character
  • You can match a space character with just the space character;
  • [^ ] matches anything but a space character.

Pick whichever is most appropriate.

cletus
  • 616,129
  • 168
  • 910
  • 942
0

I use either [^[:space:]] or [^ ] depending on the situation.

svin83
  • 239
  • 1
  • 4
  • 13