Main Content

characterListPattern

匹配列表中的字符

自 R2020b 起

说明

示例

pat = characterListPattern(characters) 创建与 characters 中包含的任何字符匹配的模式。

示例

pat = characterListPattern(startCharacter,endCharacter) 匹配范围在 startCharacterendCharacter 之间的任何字符,包括 startCharacterendCharacter

示例

全部折叠

定义一个模式表达式 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 仅匹配从 ag 的小写字母。从字符串中提取该模式。

txt = "ABCDEFGHIJKLMONPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
pat = characterListPattern("a","g");
letters1 = extract(txt,pat)
letters1 = 7x1 string
    "a"
    "b"
    "c"
    "d"
    "e"
    "f"
    "g"

将 pat 创建为一个 pattern 对象,该对象使用 letterBoundarycharacterListPatternlettersPattern 匹配以元音字母开头的单词。提取该模式。

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"

输入参数

全部折叠

要匹配的字符的列表,指定为字符向量或字符串标量。

示例: pat = characterListPattern("aeiou")

要匹配的字母范围的开头字符,指定为包含单个字符的字符标量或字符串标量。

示例: pat = characterListPattern("a","d")

要匹配的字母范围的结尾字符,指定为包含单个字符的字符标量或字符串标量。

示例: pat = characterListPattern("C","a")

输出参量

全部折叠

模式表达式,以 pattern 对象形式返回。

详细信息

全部折叠

文本的字符顺序

MATLAB® 使用 UTF-16 字符编码方案将字符存储为 Unicode®。字符和字符串数组按 UTF-16 代码点顺序进行排序。对于同时也是 ASCII 字符的字符,此顺序意味着大写字母在小写字母之前。数字和某些标点符号也在字母之前。有关 Unicode 的详细信息,包括字符和代码值之间的映射,请参阅 Unicode

扩展功能

基于线程的环境
使用 MATLAB® backgroundPool 在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool 加快代码运行速度。

版本历史记录

在 R2020b 中推出