characterListPattern
说明
创建与 pat
= characterListPattern(characters
)characters
中包含的任何字符匹配的模式。
匹配范围在 pat
= characterListPattern(startCharacter
,endCharacter
)startCharacter
和 endCharacter
之间的任何字符,包括 startCharacter
和 endCharacter
。
示例
在文本中查找元音字母
定义一个模式表达式 pat
,它使用 characterListPattern
只匹配小写元音字母 a、e、i、o 和 u。从字符串中提取该模式。
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"
提取在某字母范围内的字母
使用 characterListPattern
提取在指定字母范围内的字母。
定义一个模式表达式 pat
,它使用 characterListPattern
仅匹配从 a
到 g
的小写字母。从字符串中提取该模式。
txt = "ABCDEFGHIJKLMONPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; pat = characterListPattern("a","g"); letters1 = extract(txt,pat)
letters1 = 7x1 string
"a"
"b"
"c"
"d"
"e"
"f"
"g"
查找以元音字母开头的单词
将 pat 创建为一个 pattern
对象,该对象使用 letterBoundary
、characterListPattern
和 lettersPattern
匹配以元音字母开头的单词。提取该模式。
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"
将人名按字母顺序分组
创建字符串 names
变量。使用 characterListPattern
创建一个匹配字符 A 到 G 的模式。使用 startsWith
查找以 A 到 G 开头的人名。对 H 到 P 和 Q 到 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"
输入参数
characters
— 要匹配的字符的列表
字符向量 | 字符串标量
要匹配的字符的列表,指定为字符向量或字符串标量。
示例: pat = characterListPattern("aeiou")
startCharacter
— 开头字符
字符标量 | 包含单个字符的字符串标量
要匹配的字母范围的开头字符,指定为包含单个字符的字符标量或字符串标量。
示例: pat = characterListPattern("a","d")
endCharacter
— 结尾字符
字符标量 | 包含单个字符的字符串标量
要匹配的字母范围的结尾字符,指定为包含单个字符的字符标量或字符串标量。
示例: pat = characterListPattern("C","a")
详细信息
扩展功能
基于线程的环境
使用 MATLAB® backgroundPool
在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool
加快代码运行速度。
版本历史记录
在 R2020b 中推出
另请参阅
pattern
| string
| contains
| extract
| alphanumericsPattern
| digitsPattern
| lettersPattern
| whitespacePattern
| wildcardPattern
| regexp
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
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)