whitespaceBoundary - Match boundary between whitespace characters and non-whitespace

  characters - MATLAB ([original](http://www.mathworks.com/help/matlab/ref/whitespaceboundary.html)) ([raw](?raw))

Main Content

Match boundary between whitespace characters and non-whitespace characters

Since R2020b

Syntax

Description

[pat](#mw%5Fc1d85728-97aa-4fd0-a97a-e70c503d2289%5Fsep%5Fmw%5F5d8462f7-c5cb-4493-9ed2-15c426e9be6f) = whitespaceBoundary creates a pattern that matches the start or end of a run of whitespace characters.whitespaceBoundary can be negated using the ~ operator. When negated, ~whitespaceBoundary matches the boundary between any two characters except at the start or end of a run of whitespaces.

example

[pat](#mw%5Fc1d85728-97aa-4fd0-a97a-e70c503d2289%5Fsep%5Fmw%5F5d8462f7-c5cb-4493-9ed2-15c426e9be6f) = whitespaceBoundary([type](#mw%5Fc1d85728-97aa-4fd0-a97a-e70c503d2289%5Fsep%5Fmw%5F3db4ca55-65f2-47cb-817d-7df607c4d0ee)) specifies whether to match at the start or end of a run of whitespaces.type can be 'start', 'end', or'either' (default).

example

Examples

collapse all

Use whitespaceBoundary to divide a string along whitespace boundaries. This pattern matches boundaries between a whitespace character and a non-whitespace character.

Create a pattern that matches any whitespace boundaries.

txt = "123 abc .?! def 456"; pat = whitespaceBoundary ;

Use replace to insert "|" characters at the matched boundaries.

ans = "123| |abc| |.?!| |def| |456"

Use the "start" and "end" options for whitespaceBoundary to match the boundary between whitespaces and non-whitespace characters.

Create a string that contains several different character types. Create a pattern that matches any characters between an "end" boundary of whitespaces and a "start" boundary.

txt = "123 abc .?! def 456"; pat = whitespaceBoundary("end") + wildcardPattern(1,inf) + whitespaceBoundary("start");

Extract the pattern.

boundaries = extract(txt,pat)

boundaries = 3×1 string "abc" ".?!" "def"

Use the ~ operator to negate whitespaceBoundary. This matches boundaries between two characters when both are whitespace characters or neither is a whitespace character.

Create a string that contains several different character types. Create a pattern that matches a negated whitespaceBoundary.

txt = "123 abc .?!"; pat = ~whitespaceBoundary;

Use replace to insert "|" characters to show where ~whitespaceBoundary matches.

boundaries = replace(txt,pat,"|")

boundaries = "|1|2|3 a|b|c .|?|!|"

Input Arguments

collapse all

Boundary type, specified as 'start', 'end', or 'either'.

Data Types: char | string

More About

collapse all

A whitespace is any character or series of characters that represent horizontal or vertical space. When rendered, a whitespace character does not correspond to a visible mark, but typically does occupy an area on a page. Common whitespace characters include:

Significant Whitespace Character Description
char(32) Standard whitespace character, ' '
char(133) Next line
char(160) Nonbreaking space
char(8199) Figure space
char(8239) Narrow no-break space

For more information, see Whitespace character.

Extended Capabilities

Version History

Introduced in R2020b