Main Content

characterListPattern

Match characters from list

Since R2020b

Description

pat = characterListPattern(characters) creates a pattern that matches any character included in characters.

example

pat = characterListPattern(startCharacter,endCharacter) matches any character in the range between startCharacter and endCharacter, including startCharacter and endCharacter.

example

Examples

collapse all

Define a pattern expression, pat, that matches only the lowercase 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 = 10×1 string
    "e"
    "e"
    "e"
    "a"
    "e"
    "e"
    "e"
    "a"
    "o"
    "e"

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 = 7×1 string
    "a"
    "b"
    "c"
    "d"
    "e"
    "f"
    "g"

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 = 5×1 string
    "armadillo"
    "echidna"
    "iguana"
    "ostrich"
    "unicorn"

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 = 1×7 string
    "Andres"    "Betty"    "Chris"    "David"    "Etsuko"    "Fredrick"    "Gaston"

NameGroup2 = names(startsWith(names,characterListPattern('H','P')))    
NameGroup2 = 1×9 string
    "Hasina"    "Ian"    "Jose"    "Karen"    "Larry"    "Malia"    "Nick"    "Omar"    "Patrick"

NameGroup3 = names(startsWith(names,characterListPattern('Q','Z')))
NameGroup3 = 1×10 string
    "Quincy"    "Rajesh"    "Shruti"    "Tau"    "Uma"    "Veronica"    "Wendy"    "Xiao"    "Yakov"    "Zhanna"

Input Arguments

collapse all

List of characters to match, specified as a character vector or string scalar.

Example: pat = characterListPattern("aeiou")

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")

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

collapse all

Pattern expression, returned as a pattern object.

More About

collapse all

Extended Capabilities

expand all

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced in R2020b