How can I have a regular expression that tests for spaces or tabs, but not newlines?
I tried \s
, but I found out that it tests for newlines too.
I use C# (.NET) and WPF, but it shouldn't matter.
The most common forms of whitespace you will use with regular expressions are the space (␣), the tab (\t), the new line (\n) and the carriage return (\r) (useful in Windows environments), and these special characters match each of their respective whitespaces.
Use \t to match a tab character (ASCII 0x09), \r for carriage return (0x0D) and \n for line feed (0x0A).
Spaces can be found simply by putting a space character in your regex. Whitespace can be found with \s . If you want to find whitespace between words, use the \b word boundary marker.
You can stick optional whitespace characters \s* in between every other character in your regex. Although granted, it will get a bit lengthy.
Use character classes: [ \t]
Try this character set:
[ \t]
This does only match a space or a tabulator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With