Search code examples
regexmatlabuniqueedit

How do I edit a string in Matlab so that it only contains letters A-Z


I need to be able to edit a string in Matlab so that it only contains letters a-z.

Example:

If I have the words

dog 
cat
fish
°·°·°·
∞°¥È
¥©±∏≥™
¥Î„‚Ω‚‡Ó

I want to be able to edit this list so that the only words I get are

dog
cat
fish

Currently, the way I am editing the words is using regexp() as shown below.

pat = '[\s\.\]\[\&\%\#\*\,\$\_\ ,;:-''"?!/()@=><]+'; 
words = regexp(st,pat,'split');
words = lower(words);

This method works well for removing quite a bit of the symbols that I don't want but there has been a few exceptions, including the ones I listed above, that I want to remove.


Solution

  • You can try:

    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
    

    Not the most efficient or elegant thing in the world, but will probably work.

    Also, if you don't want to use loops, you can do something like:

    condition = str>='a' & str <='z'; % | ...
    string=string[condition];