Matlab - how to replace all special characters in a vector?

Is it possible to replace all special characters in a matlab vector through a regular expression?

thanks

* EDIT: *

Thank you for your responses. I am trying to achieve the following. I have a text file containing several paragraphs from a novel. I read this file in vector.

fileText = ['token1,' token_2 '' token%! 3 '], etc.

In this case _%! are special characters, and I would like to replace them with spaces (''). Can this be achieved through regular expressions? I can do this with javascript, but I can't get it to work in Matlab.

thanks

+7
regex vector matlab
source share
1 answer

If by “special characters” you mean less commonly used Unicode characters , such as ¥ , or ¼ , then you can either use the REGEXPREP function or set comparison functions such as ISMEMBER (and you can convert the character string to its equivalent integer code using the DOUBLE function if necessary). Here are a few examples where everything except the standard English characters (lower and upper case) is removed from the string:

 str = ['ABCDEFabcdefÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐ']; %# A sample string str = regexprep(str,'[^a-zA-Z]',''); %# Remove characters using regexprep str(~ismember(str,['A':'Z' 'a':'z'])) = ''; %# Remove characters using ismember %# (as suggested by Andrew) str(~ismember(double(str),[65:90 97:122])) = ''; %# Remove characters based on %# their integer code 

All the above parameters give the same result:

 str = ABCDEFabcdef 


EDIT:

In response to a specific example in the updated question here, you can use REGEXPREP to replace all characters that are not az , az or 0-9 with spaces:

 str = regexprep(str,'[^a-zA-Z0-9]',''); 

This may be easier than trying to write a regular expression to match each individual “special” character, since there could potentially be many of them. However, if you were sure that the only special characters would be _ , % and ! This should be the same as above:

 str = regexprep(str,'[_%!]',''); 

In addition, as mentioned in an Amro comment, you can also use the ISSTRPROP function to replace all non-letter characters with spaces as follows:

 str(~isstrprop(str,'alphanum')) = ''; 
+22
source share

All Articles