whitespacePattern

Pattern that matches whitespace characters.

Category: Language Fundamentals > Data Types

Syntax

pat = whitespacePattern

Matches one or more consecutive whitespace characters.

pat = whitespacePattern(n)

Matches exactly n whitespace characters.

pat = whitespacePattern(min, max)

Matches between min and max whitespace characters; max may be Inf.

Description

Spaces, tabs and line breaks all count as whitespace. Without arguments the pattern is greedy, so a run of several spaces is a single match.

It replaces the regular expression \s+ in most text-cleaning code, and can be joined to other patterns with +.

Input arguments

n
Exact number of whitespace characters to match.
min, max
Lower and upper limits on the run length.

Output arguments

pat
A pattern object to use with extract, split, replace, contains, count and similar text functions.

Example

Split on runs of whitespace

s = "a   b  c";
split(s, whitespacePattern)
count(s, whitespacePattern)
Output
ans = 3×1 string array

    "a"
    "b"
    "c"
ans = 2

See also

whitespaceBoundary wildcardPattern split strip pattern