MATLAB Answers

0

search for a word in a string with complete match

Asked by Raghunandan Venkatesh on 12 Nov 2019 at 4:35
Latest activity Commented on by Raghunandan Venkatesh on 12 Nov 2019 at 5:10
Hi all,
I have a string and a cell as follows
StringText = 'High rotation speed changes the parameter RevolutionChange'
WordCell = {'Slip' , 'GearRatio','Revolution','RevolutionChange'}
When I use regular explression to find the line parameters in StringText, the 3rd and 4th indices are obtained as outputs. I need only the 4th index to be the output
Index = regexp(StringText, WordCell, 'match')
Present output = 1×4 cell array => {0×0 cell} {0×0 cell} {Revolution} {RevolutionChange}
required output = 1×4 cell array => {0×0 cell} {0×0 cell} {0x0 cell} {RevolutionChange}
Thanks

  0 Comments

Sign in to comment.

1 Answer

Answer by Akira Agata
on 12 Nov 2019 at 5:05
 Accepted Answer

How about the following way?
StringText = 'High rotation speed changes the parameter RevolutionChange';
WordCell = {'Slip' , 'GearRatio','Revolution','RevolutionChange'};
% Split text into words
WordsInText = split(StringText);
% Find word(s) in WordCell used in the StringText
idx = ismember(WordCell,WordsInText);
% Show word(s) used in the StringText
WordCell(idx)
ans =
{'RevolutionChange'}

  1 Comment

Hi Akira,
Thanks for the answer. It works!
Just out of curiosity, Is there a way to do the same without strsplit?

Sign in to comment.