regex to match a single character that is anything but a space

Regex

Regex Problem Overview


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 Solutions


Solution 1 - Regex

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'!

Solution 2 - Regex

  • \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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRyanView Question on Stackoverflow
Solution 1 - RegexAndrew MooreView Answer on Stackoverflow
Solution 2 - RegexcletusView Answer on Stackoverflow