finding a string with parentheses using regexp

14 ビュー (過去 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 Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by