Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a regex that will match an arbitrary sequence of spaces and tabs

Tags:

regex

Could anyone help me to assemble a pattern that matches an arbitrary sequence of spaces and tabs?

like image 962
Jarek Avatar asked Feb 11 '11 17:02

Jarek


People also ask

How do you match a tab character in regex?

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

How do I match a pattern in regex?

Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" . Non-alphanumeric characters without special meaning in regex also matches itself. For example, = matches "=" ; @ matches "@" .

What does \d mean in regex?

The \D metacharacter matches non-digit characters.

How do you represent a space in regex?

\s stands for “whitespace character”. Again, which characters this actually includes, depends on the regex flavor. In all flavors discussed in this tutorial, it includes [ \t\r\n\f]. That is: \s matches a space, a tab, a carriage return, a line feed, or a form feed.


2 Answers

[ \t]+

will match arbitrary sequences (e.g., spaces followed by tabs followed by more spaces ...).

like image 122
mob Avatar answered Oct 19 '22 20:10

mob


\s+ should capture all whitespace, including spaces, tabs, carriage returns, and some weird whitespace characters. Use \s* if you want it to be optional.

like image 39
Justin Morgan Avatar answered Oct 19 '22 19:10

Justin Morgan