MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
For more information, see “Lookahead Assertions in Regular Expressions” on page 2-57.

Logical and Conditional Operators

Logical and conditional operators allow you to test the state of a given condition, and then
use the outcome to determine which pattern, if any, to match next. These operators
support logical OR and if or if/else conditions. (For AND conditions, see “Lookaround
Assertions” on page 2-51.)

Conditions can be tokens on page 2-52, lookaround assertions on page 2-51, or dynamic
expressions on page 2-54 of the form (?@cmd). Dynamic expressions must return a
logical or numeric value.

Conditional Operator Description Example
expr1|expr2 Match expression expr1 or
expression expr2.

If there is a match with expr1,
then expr2 is ignored.

'(let|tel)\w+' matches words
that start with let or tel.

(?(cond)expr) If condition cond is true, then
match expr.

'(?(?@ispc)[A-Z]:\\)'
matches a drive name, such as C:\,
when run on a Windows system.
(?(cond)expr1|
expr2)

If condition cond is true, then
match expr1. Otherwise, match
expr2.

'Mr(s?)\..*?(?(1)her|his)
\w*' matches text that includes
her when the text begins with Mrs,
or that includes his when the text
begins with Mr.

Token Operators

Tokens are portions of the matched text that you define by enclosing part of the regular
expression in parentheses. You can refer to a token by its sequence in the text (an ordinal
token), or assign names to tokens for easier code maintenance and readable output.

Ordinal Token Operator Description Example
(expr) Capture in a token the characters
that match the enclosed
expression.

'Joh?n\s(\w*)' captures a token
that contains the last name of any
person with the first name John or
Jon.

2 Program Components

Free download pdf