JoelFan JoelFan - 5 months ago 24
Perl Question

Match whitespace but not newlines

I sometimes want to match whitespace but not newline.

So far I've been resorting to

[ \t]
. Is there a less awkward way?

Answer

I am shocked that so many unnecessarily elaborate answers have been written. By far the cleanest way, which will work on version 10 and later of Perl 5, is to use the horizontal whitespace character class \h. This will match just horizontal tab and space from the ASCII set, or any of these Unicode characters.

CHARACTER TABULATION
SPACE
NO-BREAK SPACE
OGHAM SPACE MARK
MONGOLIAN VOWEL SEPARATOR
EN QUAD
EM QUAD
EN SPACE
EM SPACE
THREE-PER-EM SPACE
FOUR-PER-EM SPACE
SIX-PER-EM SPACE
FIGURE SPACE
PUNCTUATION SPACE
THIN SPACE
HAIR SPACE
NARROW NO-BREAK SPACE
MEDIUM MATHEMATICAL SPACE
IDEOGRAPHIC SPACE