Could anyone help me to assemble a pattern that matches an arbitrary sequence of spaces and tabs?
4 Answers
[ \t]+
will match arbitrary sequences (e.g., spaces followed by tabs followed by more spaces ...).
-
-
1@EndreBoth - Why would you want to escape the
+
? It's being used as a quantifier, not trying to match a literal+
character. Are you talking about a particular language's flavor of regex? Jul 11, 2018 at 13:46 -
1Thanks @JustinMorgan, I had probably mistaken this for a Vim question. Apologies. Jul 11, 2018 at 18:56
-
4
\s+
should capture all whitespace, including spaces, tabs, carriage returns, and some weird whitespace characters. Use \s*
if you want it to be optional.
-
2Great answer! Note, this tool is really helpful to verify the meaning of regular expressions: regex101.com. Feb 23, 2019 at 23:31
( |\t)+
will match a sequence of one or more spaces or tabs, is that what you're looking for ?
To piggy-back on @Justin Morgan's answer, note that according to https://regex101.com/, \s+
is equal to [\r\n\t\f\v ]+
.
Therefore, if your system doesn't support the \s+
(or \s*
) construct, use [\r\n\t\f\v ]
in place of the \s
part instead.