Main Content

digitsPattern

Match digit characters

Description

example

pat = digitsPattern creates a pattern that matches text composed of one or more digit characters. digitsPattern is Unicode tolerant.

example

pat = digitsPattern(N) matches text composed of exactly N digit characters.

example

pat = digitsPattern(minCharacters,maxCharacters) matches text composed of a quantity of digit characters greater than or equal to minCharacters and less than or equal to maxCharacters. inf is a valid value for maxCharacters. digitsPattern is greedy and matches a quantity of digit characters as close to maxCharacters as possible.

Examples

collapse all

Define a pattern expression, pat, for numeric digits using digitsPattern. Extract numeric digits from the string.

str = "MathWorks was founded in 1984. Patterns were 1st introduced in R2020b.";
pat = digitsPattern;
year = extract(str,pat)
year = 3x1 string
    "1984"
    "1"
    "2020"

Define pat as a pattern with 4 numeric digits. Use pat to extract the years from the string array.

dates = ["November 4th 2015" "12/14/2019" "Jan. 1 2020" "28-Jan-2020 15:28:58"];
pat = digitsPattern(4);
year_array = extract(dates,pat)
year_array = 1x4 string
    "2015"    "2019"    "2020"    "2020"

Define pat as a pattern of numbers with between 3 and 4 digits. Use pat to extract the three parts of a U.S. phone number from a string array.

str = "My phone number is 1-(555)-123-4567";
pat = digitsPattern(3,4);
phoneNum = extract(str,pat)
phoneNum = 3x1 string
    "555"
    "123"
    "4567"

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