ans =
'g11n'
Using a dynamic expression ${num2str(length($2))} enables you to base the
replacement expression on the input text so that you do not have to change the
expression each time. This example uses the dynamic replacement syntax ${cmd}.
match_expr = '(^\w)(\w*)(\w$)';
replace_expr = '$1${num2str(length($2))}$3';
regexprep('internationalization', match_expr, replace_expr)
ans =
'i18n'
regexprep('globalization', match_expr, replace_expr)
ans =
'g11n'
When parsed, a dynamic expression must correspond to a complete, valid regular
expression. In addition, dynamic match expressions that use the backslash escape
character (\) require two backslashes: one for the initial parsing of the expression, and
one for the complete match. The parentheses that enclose dynamic expressions do not
create a capturing group.
There are three forms of dynamic expressions that you can use in match expressions, and
one form for replacement expressions, as described in the following sections
Dynamic Match Expressions — (??expr)
The (??expr) operator parses expression expr, and inserts the results back into the
match expression. MATLAB then evaluates the modified match expression.
Here is an example of the type of expression that you can use with this operator:
chr = {'5XXXXX', '8XXXXXXXX', '1X'};
regexp(chr, '^(\d+)(??X{$1})$', 'match', 'once');
The purpose of this particular command is to locate a series of X characters in each of the
character vectors stored in the input cell array. Note however that the number of Xs
2 Program Components