Match regular expression (case insensitive)
collapse all in page
Syntax
startIndex = regexpi(str,expression)
[startIndex,endIndex]= regexpi(str,expression)
out = regexpi(str,expression,outkey)
[out1,...,outN]= regexpi(str,expression,outkey1,...,outkeyN)
___ = regexpi(___,option1,...,optionM)
___ = regexpi(___,'forceCellOutput')
Description
startIndex = regexpi(str,expression)
returnsthe starting index of each substring of str
thatmatches the character patterns specified by the regular expression,without regard to letter case. If there are no matches, startIndex
isan empty array.
example
[startIndex,endIndex]= regexpi(str,expression)
returnsthe starting and ending indices of all matches.
out = regexpi(str,expression,outkey)
returnsthe output specified by outkey
. For example,if outkey
is 'match'
, then regexpi
returnsthe substrings that match the expression rather than their startingindices.
[out1,...,outN]= regexpi(str,expression,outkey1,...,outkeyN)
returnsthe outputs specified by multiple output keywords, in the specifiedorder. For example, if you specify 'match'
,'tokens'
,then regexpi
returns substrings that match theentire expression and tokens thatmatch parts of the expression.
___ = regexpi(___,option1,...,optionM)
modifiesthe search using the specified option flags. For example, specify 'matchcase'
toperform a case-sensitive match. You can include any of the inputsand request any of the outputs from previous syntaxes.
example
___ = regexpi(___,'forceCellOutput')
returnseach output argument as a scalar cell. The cells contain the numericarrays or substrings that are described as the outputs of the previoussyntaxes. You can include any of the inputs and request any of theoutputs from previous syntaxes.
Examples
collapse all
Pattern Matching
Open Live Script
Find words that start with c
, end with t
, and contain one or more vowels between them.
str = 'bat cat can car COAT court cut ct CAT-scan';expression = 'c[aeiou]+t';startIndex = regexpi(str,expression)
startIndex = 1×4 5 17 28 35
Values in startIndex
indicate the index of the first character of each word that matches the regular expression.
The regular expression 'c[aeiou]+t'
specifies this pattern:
c
must be the first character.c
must be followed by one of the characters inside the brackets,[aeiou]
.The bracketed pattern must occur one or more times, as indicated by the
+
operator.t
must be the last character, with no characters between the bracketed pattern and thet
.
Case-Sensitive Match
Open Live Script
Match letter case in all or part of an expression.
By default, regexpi
performs case-insensitive matching.
str = 'A character vector with UPPERCASE and lowercase text.';expression = '\w*case';matchStr = regexpi(str,expression,'match')
matchStr = 1x2 cell {'UPPERCASE'} {'lowercase'}
Use the regexp
function with the same syntax as regexpi
to perform case-sensitive matching.
matchWithRegexp = regexp(str,expression,'match')
matchWithRegexp = 1x1 cell array {'lowercase'}
To disable case-sensitive matching for regexp
, use the 'ignorecase'
option.
matchWithIgnorecase = regexp(str,expression,'match','ignorecase')
matchWithIgnorecase = 1x2 cell {'UPPERCASE'} {'lowercase'}
For multiple expressions, enable and disable case-insensitive matching for selected expressions using the (?i)
and (?-i)
search flags.
expression = {'(?-i)\w*case';... '(?i)\w*case'};matchStr = regexp(str,expression,'match');celldisp(matchStr)
matchStr{1}{1} = lowercase matchStr{2}{1} = UPPERCASE matchStr{2}{2} = lowercase
Input Arguments
collapse all
option
— Search option
'once'
| 'warnings'
| 'matchcase'
| 'emptymatch'
| 'dotexceptnewline'
| 'lineanchors'
| ...
Search option, specified as a character vector. Options come in pairs: one option that corresponds to the default behavior, and one option that allows you to override the default. Specify only one option from a pair. Options can appear in any order.
Default | Override | Description |
---|---|---|
|
| Match the expression as many times as possible (default), or only once. |
|
| Suppress warnings (default), or display them. |
|
| Ignore letter case (default), or match case. |
|
| Ignore zero length matches (default), or include them. |
|
| Match dot with any character (default), or all except newline ( |
|
| Apply |
|
| Include space characters and comments when matching (default), or ignore them. With |
Data Types: char
Output Arguments
collapse all
More About
collapse all
Tokens
Tokens are portions of the matched text thatcorrespond to portions of the regular expression. To create tokens,enclose part of the regular expression in parentheses.
For example, this expression finds a date of the form dd-mmm-yyyy
,including tokens for the day, month, and year.
str = 'Here is a date: 01-Apr-2020';expression = '(\d+)-(\w+)-(\d+)';mydate = regexp(str,expression,'tokens');mydate{:}
ans = 1×3 cell array {'01'} {'Apr'} {'2020'}
You can associate names with tokens so that they are more easilyidentifiable:
str = 'Here is a date: 01-Apr-2020';expression = '(?<day>\d+)-(?<month>\w+)-(?<year>\d+)';mydate = regexp(str,expression,'names')
mydate = struct with fields: day: '01' month: 'Apr' year: '2020'
For more information, see Tokens in Regular Expressions.
Extended Capabilities
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version History
Introduced before R2006a
See Also
regexp | regexprep | regexptranslate | strfind | strjoin | strsplit | strrep | contains | replace
Topics
- Lookahead Assertions in Regular Expressions
- Dynamic Regular Expressions
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office