Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression: find spaces (tabs/space), but not newlines

Tags:

regex

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.

like image 750
Jiew Meng Avatar asked Aug 27 '10 10:08

Jiew Meng


People also ask

Is tab a whitespace for regex?

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.

How do you match a tab in regex?

Use \t to match a tab character (ASCII 0x09), \r for carriage return (0x0D) and \n for line feed (0x0A).

How do you find a space in regex?

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.

How do you skip a space in regex?

You can stick optional whitespace characters \s* in between every other character in your regex. Although granted, it will get a bit lengthy.


2 Answers

Use character classes: [ \t]

like image 94
Lekensteyn Avatar answered Sep 19 '22 15:09

Lekensteyn


Try this character set:

[ \t] 

This does only match a space or a tabulator.

like image 42
Gumbo Avatar answered Sep 19 '22 15:09

Gumbo