regex - 如何在 Matlab 中编辑字符串,使其仅包含字母 A-Z

标签 regex matlab unique edit

我需要能够在 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/

相关文章:

python - Numpy:删除 2x2 数组中相邻的重复子数组?

laravel - 如何将唯一约束添加到 laravel 中现有的 foreignId 列?

python - 如何在 PDF 文档中搜索公司名称或股票代码?

regex - 编译正则表达式时会发生什么?

function - 如何更正 "Function definitions are not permitted at the prompt or in scripts"

python - 是否存在重载阶跃函数?

Redis唯一增量

regex - 在 sed 语句中组合多个正则表达式

javascript - 正则表达式为单个匹配提供多个匹配是否正常?

matlab - 如何让 Google Protobuf 在 Matlab 中工作?