0

I am trying to use regexp in MATLAB to search for two words in strings in cell arrays. My cell array contains

strings={'1abc_2def_ghi_AB_12A','1abc_2def_ghi_BD_19A','1abc_2def_ghi_CD_16A',}

How would I go about constructing the expression to search the cell array for the string that contains both 'ghi' and '12'?

Thanks in advance for any assistance.

2
  • Where is your problem implementing this? Finding a matching regular expression or iterating the cell? Commented Mar 30, 2015 at 22:40
  • My problem is finding a matching regular expression. Commented Mar 30, 2015 at 23:11

1 Answer 1

1

How about this?

result = find(~cellfun(@isempty, regexp(strings, 'ghi')) & ...
    ~cellfun(@isempty, regexp(strings, 'AB')));

Or, using a single regular expression,

result = find(~cellfun(@isempty, regexp(strings, '(ghi.*AB|ghi.*AB)')));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.