Main Content

alphanumericBoundary

Match boundary between alphanumeric and non-alphanumeric characters

Since R2020b

Description

example

pat = alphanumericBoundary creates a pattern that matches the start or end of a run of letters and numbers. alphanumericBoundary can be negated using the ~ operator. When negated, ~alphanumericBoundary matches the boundary between any two characters except at the start or end of a run of alphanumerics.

example

pat = alphanumericBoundary(type) specifies whether to match at the start or end of a run of letters and numbers. type can be 'start', 'end', or 'either' (default).

Examples

collapse all

Use alphanumericBoundary to divide text along boundaries between letters and digits and non-alphanumeric characters.

txt = "123abc .?! def456";
pat = alphanumericBoundary;

Use replace to insert "|" characters at the matched boundaries. This operation shows where these boundaries are in the text that contains a variety of character types.

replace(txt,pat,"|")
ans = 
"|123abc| .?! |def456|"

Use the "start" and "end" options for alphanumericBoundary to match the boundary between letters and digits and non-alphanumeric characters.

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

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

Extract the pattern.

boundaries = extract(txt,pat)
boundaries = 4x1 string
    "123"
    "abc"
    "def"
    "456"

Use the ~ operator to negate alphanumericBoundary. This pattern matches the boundary between two characters when both are alphanumeric characters or neither character is alphanumeric.

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

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

Use replace to insert "|" characters to show where ~alphanumericBoundary 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

Output Arguments

collapse all

Pattern expression, returned as a pattern object.

Extended Capabilities

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced in R2020b