Exact string match to confirm the string existence in the file

7 ビュー (過去 30 日間)
Bhaskar R
Bhaskar R 2019 年 12 月 17 日
コメント済み: Bhaskar R 2019 年 12 月 17 日
Hi all,
Could you find the exact string match in the input file?
I am unable get exact match of the sting in the read file, i tried this using strfind command, but could not get what i want.
My case is as follows,
Input file has data, out of it I need to find specific exact string in the file, suppose my input file name is input_data.txt and the data
>> txt = fileread('input_data.txt')
>> txt =
'var_abc = 12;
var_abcd = 20;
var_xyz = 30;
var_xy = 152;
var_abcxyz = 142;'
In this data i need to confirm specific string is present or not in the file.
Suppose i want to search the string var_ab in the file it's giving me
strfind(t, 'var_ab')
ans =
1 16 62
but it is finding strings within other strings, here I need to search only exact specific string var_ab
If the specificr string is present in the file, it should return true/value/string else an empty/false output
i.e
strfind(t, 'var_ab')
ans =
[]
-Thanks

採用された回答

Stephen23
Stephen23 2019 年 12 月 17 日
編集済み: Stephen23 2019 年 12 月 17 日
>> txt = fileread('input_data.txt');
>> spl = regexp(txt,'\w+','match');
>> any(strcmp('var_ab',spl(1:2:end)))
ans = 0
>> any(strcmp('var_abc',spl(1:2:end)))
ans = 1

その他の回答 (1 件)

Shunichi Kusano
Shunichi Kusano 2019 年 12 月 17 日
Hi Bhaskar,
how about including space in the reference string? For your case, 'var_ab ', not 'var_ab'.
I wonder if I could catch your point.
HTH.
  1 件のコメント
Bhaskar R
Bhaskar R 2019 年 12 月 17 日
Yes, it may include but i used string trim. Stephen Cobeldick's answer helped.
Thanks for the reply.

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by