Main Content

characterListPattern

Match characters from list

Description

example

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.

Examples

collapse all

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"

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"

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"

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
  Columns 1 through 6

    "Andres"    "Betty"    "Chris"    "David"    "Etsuko"    "Fredrick"

  Column 7

    "Gaston"

NameGroup2 = names(startsWith(names,characterListPattern('H','P')))    
NameGroup2 = 1x9 string
  Columns 1 through 7

    "Hasina"    "Ian"    "Jose"    "Karen"    "Larry"    "Malia"    "Nick"

  Columns 8 through 9

    "Omar"    "Patrick"

NameGroup3 = names(startsWith(names,characterListPattern('Q','Z')))
NameGroup3 = 1x10 string
  Columns 1 through 7

    "Quincy"    "Rajesh"    "Shruti"    "Tau"    "Uma"    "Veronica"    "Wendy"

  Columns 8 through 10

    "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

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.

Introduced in R2020b