# MATLAB: How to compare two strings, ignoring any white space or punctuation characters

MATLAB

I would like to compare two strings. The strings may have varying numbers of spaces and punctuation characters (not necessarily at the beginning or end of the string), which I would like to ignore.

a = 'test';b = 'te s.t';%Create a regular expression%This expression matches any character except a whitespace, comma, period, semicolon, or colon exp = '[^ \f\n\r\t\v.,;:]*';%Find the matches within the stringb1 = regexp(b, exp, 'match');%Concatenate all matches into a single stringb1 = [b1{:}];%Repeat above for the other stringa1 = regexp(a, exp, 'match');a1 = [a1{:}];%Compare the modified stringsmatch = strcmp(a1, b1)