Main Content

caseInsensitivePattern

Match pattern regardless of case

Description

example

newpat = caseInsensitivePattern(pat) creates a pattern that matches pat regardless of case.

Examples

collapse all

Use caseInsensitivePattern to match text regardless of whether the text is upper-case or lower-case.

Create txt as a string with "abc" repeated in a mixture of upper- and lower-case. Create a pattern, pat, that matches the literal "abc". Extract the pattern.

txt = "abc ABC AbC";
pat = "abc";
extract(txt,pat)
ans = 
"abc"

Make pat case insensitive using caseInsensitivePattern. Extract the new pattern.

pat = caseInsensitivePattern(pat);
extract(txt,pat)
ans = 3x1 string
    "abc"
    "ABC"
    "AbC"

Use caseInsensitivePattern to enforce case insensitivity in case-sensitive functions.

Create a string of lowercase letters. Create a pattern that matches uppercase "ABC". When used in a case-sensitive function, pat does not match txt.

txt = "abc";
pat = "ABC";
contains(txt,pat,'IgnoreCase',false)
ans = logical
   0

Use caseInsensitivePattern to enforce case insensitivity in matching for pat even when used as the input for a case-sensitive function.

pat = caseInsensitivePattern(pat);
contains(txt,pat,'IgnoreCase',false)
ans = logical
   1

Input Arguments

collapse all

Input pattern, specified as a pattern, string array, character vector, or cell array of character vectors.

Data Types: char | string | pattern | cell

Output Arguments

collapse all

Output pattern, returned as a pattern or an array of pattern objects.

Algorithms

When caseInsensitivePattern and caseSensitivePattern are used as inputs for one another, the behavior of the input pattern overrules the outer function. For example, caseInsensitivePattern(caseSensitivePattern(pat)) behaves case sensitively.

Introduced in R2020b