mat*_*ber 2 regex string matlab
我'12hjb42&34ni3&(*&'在MATLAB中有一串这样的字符.
我想通过正则表达式或其他更简单的方法将数字和字母以及其他所有内容分开.我怎样才能做到这一点?
我认为使用ISSTRPROP函数会更容易,而不是使用正则表达式:
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
Run Code Online (Sandbox Code Playgroud)
哪个会给你这些结果:
alphaStr = 'hjbni'
digitStr = '1242343'
otherStr = '&&(*&'
Run Code Online (Sandbox Code Playgroud)
如果你真的想使用REGEXP,你可以这样做:
matches = regexp(str,{'[a-zA-Z]','\d','[^a-zA-Z\d]'},'match');
alphaStr = [matches{1}{:}];
digitStr = [matches{2}{:}];
otherStr = [matches{3}{:}];
Run Code Online (Sandbox Code Playgroud)