digits = '[Inf,NaN,pi,\t,\n,\d,\+,\-,\*,\.,e,i, ,E,I,\[,\],\;,\,]';
s = regexprep(str, digits, ''); % remove all the digits and other allowed characters
I found this when one of my LeafNode called "IN" was recognized as "NaN".
I suggest the following modification.
>> regexprep('IN','[Inf,NaN]','')
ans =
''
>> regexprep('IN','(Inf)|(NaN)','')
ans =
IN
One other thing is "num = str2num(str)" is a somewhat dangerous function. I'm not sure if it worth it to use this to provide some fancy functionality.