alphanumericBoundary - Match boundary between alphanumeric and non-alphanumeric characters - MATLAB (original) (raw)
Main Content
Match boundary between alphanumeric and non-alphanumeric characters
Since R2020b
Syntax
Description
[pat](#mw%5F1d7aadb7-e5a9-47c9-bd27-6b5ceb0b56b9%5Fsep%5Fmw%5F5d8462f7-c5cb-4493-9ed2-15c426e9be6f) = 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.
[pat](#mw%5F1d7aadb7-e5a9-47c9-bd27-6b5ceb0b56b9%5Fsep%5Fmw%5F5d8462f7-c5cb-4493-9ed2-15c426e9be6f) = alphanumericBoundary([type](#mw%5F1d7aadb7-e5a9-47c9-bd27-6b5ceb0b56b9%5Fsep%5Fmw%5F3db4ca55-65f2-47cb-817d-7df607c4d0ee))
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
Match Boundaries of Letters and Digits
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.
ans = "|123abc| .?! |def456|"
Match Start and End Boundaries of Letters and Digits
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"
Negating Boundaries of Letters and Digits
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
type
— Boundary type
'either'
(default) | 'start'
| 'end'
Boundary type, specified as 'start'
, 'end'
, or 'either'
.
Data Types: char
| string
Output Arguments
pat
— Pattern expression
pattern object
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