Main Content

lettersPattern

Match letter characters

Description

example

pat = lettersPattern creates a pattern that matches text composed of one or more of letters. lettersPattern is Unicode tolerant.

example

pat = lettersPattern(N) matches text composed of exactly N letters.

example

pat = lettersPattern(minCharacters,maxCharacters) matches text composed of a number of letters greater than or equal to minCharacters and less than or equal to maxCharacters. inf is a valid value for maxLetter. lettersPattern is greedy and matches a number of letters as close to maxCharacters as possible.

Examples

collapse all

Use lettersPattern to extract words from a string containing letters, numbers, white spaces, and punctuation.

Create txt as a string. Create pat as a pattern object that matches letters using lettersPattern. Extract the pattern from txt.

txt = "The 2 parties agreed. The meeting would occur at 1 PM.";
pat = lettersPattern;
words = extract(txt,pat)
words = 9x1 string
    "The"
    "parties"
    "agreed"
    "The"
    "meeting"
    "would"
    "occur"
    "at"
    "PM"

Use lettersPattern to count the occurrences of individual letters in a line of text.

Create txt as a string. Create pat as a pattern object that matches individual letters using lettersPattern. Extract the pattern. Display a histogram of the number of occurrences of each letter.

txt = "What are the letters in this sentence?";
pat = lettersPattern(1);
letters = extract(txt,pat);
letters = categorical(letters);
histogram(letters)

Use lettersPattern to exclude words with more than five letters.

Create pat as a pattern that matches groups of five or more letters using lettersPattern. Extract the pattern.

txt = "Some of these words are longer than others.";
pat = lettersPattern(5,inf);
extract(txt,pat)
ans = 4x1 string
    "these"
    "words"
    "longer"
    "others"

Input Arguments

collapse all

Number of characters to match, specified as a nonnegative integer scalar.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Minimum number of characters to match, specified as a nonnegative integer scalar.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Maximum number of characters to match, specified as a nonnegative integer scalar.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

collapse all

Pattern expression, returned as a pattern object.

Introduced in R2020b