MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

3 Basic Program Components • “String Replacement” on page 3-67 • “Handling Multiple Strings” on page 3-69 • “Operator Summary” on page 3-72 MATLAB Regular Expression Functions Several MATLAB functions support searching and replacing characters using regular expressions: Function regexp regexpi regexprep regexptranslate Description Match regular expression. Match regular expression, ignoring case. Replace string using regular expression. Translate string into regular expression. See the function reference pages to obtain more information on these functions. For more information on how to use regular expressions in general, consult a reference on that subject. The regexp and regexpi functions return up to six outputs in the order shown in the reference page for regexp. You can select specific outputs to be returned by using one or more of the following qualifiers with these commands: Qualifier 'start' 'end' 'tokenExtents' 'match' 'tokens' 'names' Value Returned Starting index of each substring matching the expression Ending index of each substring matching the expression Starting and ending indices of each substring matching a token in the expression Text of each substring matching the expression Text of each token captured Name and text of each named token captured 3-32

Regular Expressions There is an additional qualifier named 'once' that you can use to return only the first match found. Elements of an Expression Tables and examples in the sections that follow show the metacharacters and syntax supported by the regexp, regexpi, andregexprep functions in MATLAB. Expressions shown in the left column have special meaning and match one or more characters according to the usage described in the right column. Any character not having a special meaning, for example, any alphabetic character, matches that same character literally. To force one of the regular expression functions to interpret a sequence of characters literally (ratherthanasanoperator)usetheregexptranslate function. These elements are presented under these categories: • “Character Classes” on page 3-33 • “Character Representation” on page 3-36 • “Grouping Operators” on page 3-37 • “Nonmatching Operators” on page 3-39 • “Positional Operators” on page 3-40 • “Lookaround Operators” on page 3-41 • “Quantifiers” on page 3-45 • “Tokens” on page 3-48 • “Named Capture” on page 3-53 • “Conditional Expressions” on page 3-55 • “Dynamic Regular Expressions” on page 3-58 Each table is followed by a set of examples that show how to use the syntax presented in that table. Character Classes Character classes represent either a specific set of characters (e.g., uppercase) or a certain type of character (e.g., non-whitespace). 3-33

3 Basic Program Components<br />

• “String Replacement” on page 3-67<br />

• “Handling Multiple Strings” on page 3-69<br />

• “Operator Summary” on page 3-72<br />

<strong>MATLAB</strong> Regular Expression Functions<br />

Several <strong>MATLAB</strong> functions support searching and replacing characters using<br />

regular expressions:<br />

Function<br />

regexp<br />

regexpi<br />

regexprep<br />

regexptranslate<br />

Description<br />

Match regular expression.<br />

Match regular expression, ignoring case.<br />

Replace string using regular expression.<br />

Translate string into regular expression.<br />

See the function reference pages to obtain more information on these<br />

functions. For more information on how to use regular expressions in general,<br />

consult a reference on that subject.<br />

The regexp and regexpi functions return up to six outputs in the order shown<br />

in the reference page for regexp. You can select specific outputs to be returned<br />

by using one or more of the following qualifiers with these commands:<br />

Qualifier<br />

'start'<br />

'end'<br />

'tokenExtents'<br />

'match'<br />

'tokens'<br />

'names'<br />

Value Returned<br />

Starting index of each substring matching the<br />

expression<br />

Ending index of each substring matching the expression<br />

Starting and ending indices of each substring matching<br />

a token in the expression<br />

Text of each substring matching the expression<br />

Text of each token captured<br />

Name and text of each named token captured<br />

3-32

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!