Is there an equivalent function to strmatch that returns the numeric index of all array elements that begin with the specified regular expression (instead of string ?
Background: I've got an array of strings called strarray. I want to filter out all the strings that do NOT have a specific strprefix. The following code that looks for indexes of an array of strings (strmatch func) for which a particular prefix exists, and then builds new array from the lines that contain the prefix:
indexes = [];
n = strmatch(strprefix, strarray);
indexes = [indexes,n];
indexes = sort(indexes);
newarray = strarray(indexes);
It works OK, however the prefix is a string and I'd like to use defined regular expression instead.
Or perhaps there's an easier way (one liner?) to do such task ?
Update
I am aware of the regexp function. I am trying to filter out strings from an array of string, but I struggle to do it in one or two steps. My current code to do this (not sure if it's the correct Matlab way of coding).
- Step 1. Empty the string with no prefix:
regexp(strarray,[prefix,'.*'],'match','once'); - Step 2. Get index of empty lines
emptyCells = cellfun(@isempty,array); - Step 3. Remove the empty rows
array(emptyCells) = [];