这是使用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