Main Content

alphanumericsPattern

Match letter and digit characters

Since R2020b

Description

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

example

pat = alphanumericsPattern(N) matches exactly N letter or digit characters.

example

pat = alphanumericsPattern(minCharacters,maxCharacters) matches text composed of at least minCharacters and at most maxCharacters characters. inf is a valid value for maxCharacters. alphanumericsPattern is greedy and matches a number of letter and digit characters as close to maxCharacters as possible.

example

Examples

collapse all

Use alphanumericPattern to extract letters and digits from text.

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

txt = "abc 123 =+-*/";
pat = alphanumericsPattern;
alphanumerics = extract(txt,pat)
alphanumerics = 2x1 string
    "abc"
    "123"

Create txt as a string. Create pat as a pattern object that matches groups of three letters and numbers using alphanumericPattern. Extract the pattern from txt.

txt = "abcde123456_@#$";
pat = alphanumericsPattern(3);
alphanumerics = extract(txt,pat)
alphanumerics = 3x1 string
    "abc"
    "de1"
    "234"

Use alphanumericsPattern to match sets of letters and digits with a size that falls into specified range.

Create txt as a string. Create pat as a pattern object that matches groups of two to four letters and numbers using alphanumericPattern. Extract the pattern from txt.

txt = "12345ABCDE";
pat = alphanumericsPattern(2,4);

alphanumerics = extract(txt,pat)
alphanumerics = 3x1 string
    "1234"
    "5ABC"
    "DE"

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