带有 $ 符号的 MatLab Regexprep 语法



这是使用regexprep的行。

line = regexprep(line,'(,([^0-9])',' , $1');

$1语法是什么意思?

regexprep提供的替换字符串中的$1引用正则表达式中的第一个匹配标记。

因此,例如,如果我们匹配两个令牌,我们可以将匹配的字符串替换为第一个令牌之一

regexprep('abcdefgh', '(ab)(cd)', '$1')
% abefgh

第二个令牌

regexprep('abcdefgh', '(ab)(cd)', '$2')
%   cdefgh

或两者兼而有之

regexprep('abcdefgh', '(ab)(cd)', '$1$2')
%   abcdefgh

在您的示例中,与([^0-9])匹配的部分是$1引用的令牌。您发布的代码从字符串中删除(,并将其替换为,,并且$1使匹配的其余部分保持不变。

line = 'abcd(,abcd';
regexprep(line,'(,([^0-9])',' , $1')
%   abcd , abcd

最新更新