如何在 MATLAB 中将字符串解析为字母、数字等?

2024-03-01

我有一串这样的字符'12hjb42&34ni3&(*&'在 MATLAB 中。

我想通过正则表达式或其他更简单的方法来分隔数字和字母以及其他所有内容。我怎样才能做到这一点?


我认为使用函数代替正则表达式会更容易ISSTRPROP http://www.mathworks.com/help/techdoc/ref/isstrprop.html:

str = '12hjb42&34ni3&(*&';                   %# Your sample string
alphaStr = str(isstrprop(str,'alpha'));      %# Get the alphabetic characters
digitStr = str(isstrprop(str,'digit'));      %# Get the numeric characters
otherStr = str(~isstrprop(str,'alphanum'));  %# Get everything that isn't an
                                             %#   alphanumeric character

这会给你这些结果:

alphaStr = 'hjbni'
digitStr = '1242343'
otherStr = '&&(*&'

If you really想用REGEXP http://www.mathworks.com/help/techdoc/ref/regexp.html,这就是你可以做到的:

matches = regexp(str,{'[a-zA-Z]','\d','[^a-zA-Z\d]'},'match');
alphaStr = [matches{1}{:}];
digitStr = [matches{2}{:}];
otherStr = [matches{3}{:}];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 MATLAB 中将字符串解析为字母、数字等? 的相关文章

随机推荐