Main Content

lettersPattern

Match letter characters

Since R2020b

Description

pat = lettersPattern creates a pattern that matches text composed of one or more of letters. lettersPattern accepts letter characters as defined by the Unicode standard.

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.

example

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)

Figure contains an axes object. The axes object contains an object of type categoricalhistogram.

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.

Extended Capabilities

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

Version History

Introduced in R2020b