필터 지우기
필터 지우기

finding a string with parentheses using regexp

조회 수: 15 (최근 30일)
wesso Dadoyan
wesso Dadoyan 2016년 6월 22일
편집: Stephen23 2016년 6월 23일
A={'5123(P)';'5123 ESP'} how can i search A to find the cell that has the '(P)' string?
find(~cellfun(@isempty,regexp(A,'(P)')));
is giving 1 & 2 as answer which is not correct since it should be 1 only

채택된 답변

Stephen23
Stephen23 2016년 6월 22일
편집: Stephen23 2016년 6월 23일
find(~cellfun(@isempty,regexp(A,'\(P\)')));
Explanation: parentheses are special characters in regular expressions, and need to be escaped to be treated as literal parentheses. Of course the documentation is the best place to learn how to to use regular expressions properly:
Alternatively you could simply use strfind instead of regexp:
find(~cellfun(@isempty,strfind(A,'(P)')))

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by