(The $` and $ ́ operators capture that part of the text that precedes and follows the
current parsing location. You need two single-quotation marks ($'') to express the
sequence $ ́ when it appears within text.)
chr = 'abcdefghij';
expr = '(?@disp(sprintf(''starting match: [%s^%s]'',$`,$'')))g';
regexp(chr, expr, 'once');
starting match: [^abcdefghij]
starting match: [a^bcdefghij]
starting match: [ab^cdefghij]
starting match: [abc^defghij]
starting match: [abcd^efghij]
starting match: [abcde^fghij]
starting match: [abcdef^ghij]
Commands in Replacement Expressions — ${cmd}
The ${cmd} operator modifies the contents of a regular expression replacement pattern,
making this pattern adaptable to parameters in the input text that might vary from one
use to the next. As with the other dynamic expressions used in MATLAB, you can include
any number of these expressions within the overall replacement expression.
In the regexprep call shown here, the replacement pattern is '$
{convertMe($1,$2)}'. In this case, the entire replacement pattern is a dynamic
expression:
regexprep('This highway is 125 miles long', ...
'(\d+.?\d*)\W(\w+)', '${convertMe($1,$2)}');
The dynamic expression tells MATLAB to execute a function named convertMe using the
two tokens (\d+.?\d*) and (\w+), derived from the text being matched, as input
arguments in the call to convertMe. The replacement pattern requires a dynamic
expression because the values of $1 and $2 are generated at runtime.
The following example defines the file named convertMe that converts measurements
from imperial units to metric.
function valout = convertMe(valin, units)
switch(units)
case 'inches'
fun = @(in)in .* 2.54;
Dynamic Regular Expressions