lettersPattern - Match letter characters - MATLAB (original) (raw)

Main Content

Match letter characters

Since R2020b

Syntax

Description

[pat](#mw%5F48ace947-499f-4962-8302-6942418fde73%5Fsep%5Fmw%5F5d8462f7-c5cb-4493-9ed2-15c426e9be6f) = 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](#mw%5F48ace947-499f-4962-8302-6942418fde73%5Fsep%5Fmw%5F5d8462f7-c5cb-4493-9ed2-15c426e9be6f) = lettersPattern([N](#mw%5F48ace947-499f-4962-8302-6942418fde73%5Fsep%5Fmw%5Fc26bd0b8-cca4-4db7-9243-3a93e54ece82)) matches text composed of exactly N letters.

example

[pat](#mw%5F48ace947-499f-4962-8302-6942418fde73%5Fsep%5Fmw%5F5d8462f7-c5cb-4493-9ed2-15c426e9be6f) = lettersPattern([minCharacters](#mw%5F48ace947-499f-4962-8302-6942418fde73%5Fsep%5Fmw%5F96647eac-55f9-4ac1-8321-bf9c66471066),[maxCharacters](#mw%5F48ace947-499f-4962-8302-6942418fde73%5Fsep%5Fmw%5Fd6bd5b43-08f5-48d3-be21-53d61b6ee105)) matches text composed of a number of letters greater than or equal tominCharacters and less than or equal tomaxCharacters. inf is a valid value formaxLetter. lettersPattern is_greedy_ and matches a number of letters as close tomaxCharacters 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 = 9×1 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 = 4×1 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

Extended Capabilities

Version History

Introduced in R2020b