search for keywords in text file

2 ビュー (過去 30 日間)
narges
narges 2018 年 6 月 27 日
コメント済み: narges 2018 年 6 月 27 日
hi there I want to do search in TEST.txt, for 'Area' and 'class' keywords. if both exist display 'yes'. could you plz help me about that?
my code:
filetext = fileread('TEST.txt');
expr1 = '[^\n]*[Aa]rea[^\n]*';
matchstr = regexp(filetext,expr1,'match');
expr2 = '[^\n]*[Cc]lass[^\n]*';
matchstr = regexp(filetext,expr2,'match');
if matchstr(filetext,expr1)&& matchstr(filetext,expr2)
disp (yes)
end
error: Index in position 1 exceeds array bounds.

採用された回答

Guillaume
Guillaume 2018 年 6 月 27 日
  • You're overwriting your matchstring variable in the second regexp call, losing the result of the first call.
  • your matchstr(fileext, expr1) is complete nonsense. It is trying to index a scalar logical with two char arrays.
The correct expression would be:
if regexp(filetext,expr1,'match') & regexp(filetext,expr2,'match')
disp('yes');
end
It is puzzling how you could come up with the regular expressions but then write complete nonsense code.
  5 件のコメント
Paolo
Paolo 2018 年 6 月 27 日
編集済み: Paolo 2018 年 6 月 27 日
@narges You can also use regexpi instead of regexp for case insensitivity.
narges
narges 2018 年 6 月 27 日
much better. thanks a lot :)

サインインしてコメントする。

その他の回答 (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