我需要能够在 Matlab 中编辑字符串,使其仅包含字母 a-z。
示例:
如果我有话
dog
cat
fish
°·°·°·
∞°¥È
¥©±∏≥™
¥Î„‚Ω‚‡Ó
我希望能够编辑此列表,以便我得到的唯一单词是
dog
cat
fish
目前,我编辑单词的方式是使用 regexp() ,如下所示。
pat = '[\s\.\]\[\&\%\#\*\,\$\_\ ,;:-''"?!/()@=><]+';
words = regexp(st,pat,'split');
words = lower(words);
此方法可以很好地删除相当多我不想要的符号,但也有一些异常(exception),包括我上面列出的那些我想删除的符号。
最佳答案
你可以尝试:
for i=length(string):-1:1
if string[i]<int8('a') || (string[i]>int8('z') && string[i]<int8('A')) || string[i]>int8('Z')
string=[string(1:i-1) string(i+1:end);
end
end
不是世界上最高效或最优雅的东西,但可能会起作用。
此外,如果您不想使用循环,您可以执行以下操作:
condition = str>='a' & str <='z'; % | ...
string=string[condition];
关于regex - 如何在 Matlab 中编辑字符串,使其仅包含字母 A-Z,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35273600/