Info

This question is closed. Reopen it to edit or answer.

Looking for string sequence within excel table with an unidentified number of spaces inbetween characters

1 view (last 30 days)
Let's assume that I am looking for the sentence:
"This sentence has 27 characters"
inside a large excel table, which is composed on both string and numbers. However, this sentence could be written in different number os spaces inbetween the words, e.g.
"Thissentence has27 characters"
or
"Thi s sente nceha s27chara cte rs"
etc.
Is there a way to write just one command that covers all possible numbers of spaces inbetween characters, as long as the sequence always stays the same?

Answers (1)

Chris Perkins
Chris Perkins on 8 Jan 2018
Edited: Chris Perkins on 8 Jan 2018
Hi Saeid,
You can remove all spaces using "regexprep" before doing the comparison. For example,
% Setting up sample strings
testStr = 'This sentence has 27 characters';
excelStr1 = 'Th is sen tence h as 27 charac ters';
excelStr2 = 'This sent en ceha s2 7ch aracters';
% Remove spaces
testStrNoSpace = regexprep(testStr,'\s+','')
excelStr1NoSpace = regexprep(excelStr1,'\s+','')
excelStr2 = regexprep(excelStr2,'\s+','')
Here, "regexprep" is replacing each instance of one or more space characters with an empty string, effectively removing the spaces.
After stripping the space characters, you can compare your strings normally to see if they match or not.
For more information regarding "regexprep", see the following documentation link:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!