- 积分
- 32
- 注册时间
- 2009-7-29
- 仿真币
-
- 最后登录
- 1970-1-1
|
发表于 2013-7-5 20:46:22
|
显示全部楼层
来自 英国
regexprep()函数是否存在Bug?
Tokens in Replacement Strings
When using tokens in a replacement string, reference them using $1, $2, etc. instead of \1, \2, etc.
下面各例中regexprep(...)中的'$1'对应的Token是 '(\d+)',它应该是指连续的数字,
但是下面几例中 \d+ 代表的不仅仅是数字,还包含了字母。
怎么解释呢?
str={'w10n10e', 'w10s10w', 'w0n170.5w'};
regexprep(str,'(\d+)','$1') % 仅保留token 1,即 (\d+) 所指的数字。
% ans =
% 'w10n10e' 'w10s10w' 'w0n170.5w'
regexprep(str,'(\d+).*?(\d+)','$1')
% ans =
% 'w10e' 'w10w' 'w0.5w'
regexprep(str,'(\d+).*?(\d+)','$1,$2')
% ans =
% 'w10,10e' 'w10,10w' 'w0,170.5w'
regexprep(str,'w(\d+).*?(\d+)','$1,$2')
% ans =
% '10,10e' '10,10w' '0,170.5w'
regexp()函数不存在此现象。
regexp(str,'(\d+)','tokens'); celldisp(ans)
regexp(str,'(\d+).*?(\d+)','tokens'); celldisp(ans)
regexp(str,'(\d+).*?(\d+)','tokens'); celldisp(ans)
regexp(str,'w(\d+).*?(\d+)','tokens'); celldisp(ans)
输出均为数字,正常。 |
评分
-
1
查看全部评分
-
|