optionalPattern - Make pattern optional to match - MATLAB (original) (raw)
Main Content
Make pattern optional to match
Since R2020b
Syntax
Description
[newpat](#mw%5Fd8c30aba-5090-45f1-a5ae-f65f8863c423%5Fsep%5Fmw%5F2527cdca-90c8-43b1-8ea8-8fa52700322c) = optionalPattern([pat](#mw%5Fd8c30aba-5090-45f1-a5ae-f65f8863c423%5Fsep%5Fmw%5F619a0af2-3091-404f-9c61-e268bcf7fbcb))
creates a pattern that matches pat
when possible, but matchingpat
is not required for a pattern expression to match successfully. Use this function in conjunction with other pattern functions to build patterns that are more flexible in their matching requirements.
Examples
Match Patterns Optionally
Use optionalPattern
to designate a pattern as optional to match.
Create txt
as a string. Create a pattern, pat
, that matches "foo"
and will optionally match "bar"
so long as it is preceded by "foo"
. Extract the pattern.
txt = "foo bar foobar"; pat = "foo" + optionalPattern("bar"); extract(txt,pat)
ans = 2x1 string "foo" "foobar"
Match Domain and Subdomains of Email Addresses
Build a pattern that matches combinations of letters and periods after an "@"
. Use optionPattern
to match subdomains if present. If a subdomain is not present, optionalPattern
does not prevent a match if other conditions of pat
are met. Extract the pattern.
emails = ["Sue_B@nonprofit.org" "JohnDRoc12@business.com" "R.Franklin@biology.university.org"]; pat = lookBehindBoundary("@") + optionalPattern(lettersPattern + ".") + lettersPattern + "." + lettersPattern; domains = extract(emails,pat)
domains = 3x1 string "nonprofit.org" "business.com" "biology.university.org"
Input Arguments
pat
— Input pattern
pattern array | string array | character vector | cell array of character vectors
Input pattern, specified as a pattern, string array, character vector, or cell array of character vectors.
Data Types: char
| string
| pattern
| cell
Output Arguments
newpat
— Output pattern
pattern (default) | array of pattern objects
Output pattern, returned as a pattern or an array of pattern objects.
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