Search code examples
matlab

extracting strings from cellarray using regexp - matlab


c = {'The quick'; 'brown fox'; 'jumps over'; 'the lazydog'};

r = {'.*quick', '.*over'};

I'm trying to extract the strings 'The quick' and 'jumps over' using regexp. I thought this would work:

regexp(c, r, 'match')

But I'm getting the error message:

Error using regexp
Multiple strings and patterns given must have the same quantity.

Solution

  • According to the documentation:

    If str and expression are string arrays or cell arrays, they must have the same dimensions.

    so to achieve your desired result, use a single pattern with alternation:

    c = {'The quick'; 'brown fox'; 'jumps over'; 'the lazydog'};
    r = '.*quick|.*over';
    regexp(c, r, 'match')