MATLAB Programming
MATLAB Programming MATLAB Programming
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
- Page 202 and 203: 2 Data Types of an array without re
- Page 204 and 205: 2 Data Types Createa1-by-2arraywith
- Page 206 and 207: 2 Data Types ans = 7 ans = 2 ans =
- Page 208 and 209: 2 Data Types 3 4 B = reshape(A, 6,
- Page 210 and 211: 2 Data Types A{1,1} = [1 2; 3 4]; A
- Page 212 and 213: 2 Data Types [5x5 double] {2x2 cell
- Page 214 and 215: 2 Data Types G = cell(1,16); for m
- Page 216 and 217: 2 Data Types Function Handles A fun
- Page 218 and 219: 2 Data Types MATLAB Classes All MAT
- Page 220 and 221: 2 Data Types 2-120
- Page 222 and 223: 3 Basic Program Components Symbol R
- Page 224 and 225: 3 Basic Program Components Note If
- Page 226 and 227: 3 Basic Program Components Another
- Page 228 and 229: 3 Basic Program Components Verifyin
- Page 230 and 231: 3 Basic Program Components function
- Page 232 and 233: 3 Basic Program Components Evaluati
- Page 234 and 235: 3 Basic Program Components Keywords
- Page 236 and 237: 3 Basic Program Components Hereares
- Page 238 and 239: 3 Basic Program Components Operator
- Page 240 and 241: 3 Basic Program Components To test
- Page 242 and 243: 3 Basic Program Components Function
- Page 244 and 245: 3 Basic Program Components 5 11 10
- Page 246 and 247: 3 Basic Program Components Advantag
- Page 248 and 249: 3 Basic Program Components MATLAB E
- Page 250 and 251: 3 Basic Program Components This M-f
- Page 254 and 255: 3 Basic Program Components Operator
- Page 256 and 257: 3 Basic Program Components Word and
- Page 258 and 259: 3 Basic Program Components There ar
- Page 260 and 261: 3 Basic Program Components regexp(p
- Page 262 and 263: 3 Basic Program Components s = e =
- Page 264 and 265: 3 Basic Program Components ¥ dolla
- Page 266 and 267: 3 Basic Program Components Operator
- Page 268 and 269: 3 Basic Program Components Exact, M
- Page 270 and 271: 3 Basic Program Components As a sim
- Page 272 and 273: 3 Basic Program Components ans = De
- Page 274 and 275: 3 Basic Program Components Operator
- Page 276 and 277: 3 Basic Program Components Operator
- Page 278 and 279: 3 Basic Program Components Conditio
- Page 280 and 281: 3 Basic Program Components ans = g1
- Page 282 and 283: 3 Basic Program Components The dyna
- Page 284 and 285: 3 Basic Program Components at each
- Page 286 and 287: 3 Basic Program Components function
- Page 288 and 289: 3 Basic Program Components Operator
- Page 290 and 291: 3 Basic Program Components Find con
- Page 292 and 293: 3 Basic Program Components statemen
- Page 294 and 295: 3 Basic Program Components Characte
- Page 296 and 297: 3 Basic Program Components Quantifi
- Page 298 and 299: 3 Basic Program Components Named To
- Page 300 and 301: 3 Basic Program Components Comma-Se
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