characterListPattern
Description
creates a pattern that matches any character included in
pat
= characterListPattern(characters
)characters
.
matches any character in the range between pat
= characterListPattern(startCharacter
,endCharacter
)startCharacter
and
endCharacter
, including startCharacter
and
endCharacter
.
Examples
Find Vowels in Text
Define a pattern expression, pat
, that matches only the lower case vowels a, e, i, o, and u using characterListPattern
. Extract the pattern from the string.
txt = "She sells sea shells by the sea shore."; pat = characterListPattern("aeiou"); vowels = extract(txt,pat)
vowels = 10x1 string
"e"
"e"
"e"
"a"
"e"
"e"
"e"
"a"
"o"
"e"
Extract Letters from an Alphabetical Range
Use characterListPattern
to extract letters falling within a specified alphabetical range.
Define a pattern expression, pat
, that matches only the lowercase letters from a
to g
using characterListPattern
. Extract the pattern from the string.
txt = "ABCDEFGHIJKLMONPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; pat = characterListPattern("a","g"); letters1 = extract(txt,pat)
letters1 = 7x1 string
"a"
"b"
"c"
"d"
"e"
"f"
"g"
Find Words That Start with Vowels
Create pat as a pattern
object that matches words beginning with vowels using letterBoundary
, characterListPattern
, and lettersPattern
. Extract the pattern.
txt = "Do you like words like armadillo, echidna, iguana, ostrich, & unicorn?"; pat = letterBoundary + characterListPattern("aeiou") + lettersPattern; words = extract(txt,pat)
words = 5x1 string
"armadillo"
"echidna"
"iguana"
"ostrich"
"unicorn"
Sort Names into Alphabetical Groups
Create names
as a string. Use characterListPattern
to create a pattern that matches characters A through G. Find names that start with A through G using startsWith
. Repeat this process for H through P and Q through Z.
names = ["Andres" "Betty" "Chris" "David" "Etsuko" "Fredrick"... "Gaston" "Hasina" "Ian" "Jose" "Karen" "Larry" "Malia"... "Nick" "Omar" "Patrick" "Quincy" "Rajesh" "Shruti"... "Tau" "Uma" "Veronica" "Wendy" "Xiao"... "Yakov" "Zhanna"]; NameGroup1 = names(startsWith(names,characterListPattern('A','G')))
NameGroup1 = 1x7 string
"Andres" "Betty" "Chris" "David" "Etsuko" "Fredrick" "Gaston"
NameGroup2 = names(startsWith(names,characterListPattern('H','P')))
NameGroup2 = 1x9 string
"Hasina" "Ian" "Jose" "Karen" "Larry" "Malia" "Nick" "Omar" "Patrick"
NameGroup3 = names(startsWith(names,characterListPattern('Q','Z')))
NameGroup3 = 1x10 string
"Quincy" "Rajesh" "Shruti" "Tau" "Uma" "Veronica" "Wendy" "Xiao" "Yakov" "Zhanna"
Input Arguments
characters
— List of characters to match
character vector | string scalar
List of characters to match, specified as a character vector or string scalar.
Example: pat = characterListPattern("aeiou")
startCharacter
— Starting character
character scalar | string scalar containing a single character
Starting character of range of letters to match, specified as a character scalar or a string scalar containing a single character.
Example: pat = characterListPattern("a","d")
endCharacter
— Ending character
character scalar | string scalar containing a single character
Ending character of range of letters to match, specified as a character scalar or a string scalar containing a single character.
Example: pat = characterListPattern("C","a")
Output Arguments
pat
— Pattern expression
pattern object
Pattern expression, returned as a pattern
object.
More About
Character Order for Text
MATLAB® stores characters as Unicode® using the UTF-16 character encoding scheme. Character and string arrays are ordered according to the UTF-16 code point order. For the characters that are also the ASCII characters, this order means that uppercase letters come before lowercase letters. Digits and some punctuation also come before letters. For more information on Unicode, including mappings between characters and code values, see Unicode.
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
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)