Search code examples
stringmatlabsubstringcell-array

Check any string from a set of substrings available in main String in MATLAB


We have set of sub strings t = (b6,b7,y7,y8) and a main String K ='hgtb6ju\u'.

I need to check whether any of the element in t available in K. If yes, which substring(s).


Solution

  • t = {'b6', 'b7', 'y7', 'y8', 'ju'};
    K = 'hgtb6ju\u';
    logidx = ~cellfun(@isempty,regexp(K,t)); %Finding if substrings are present
    matched = t(logidx)  % Finding which substrings are present