How to search for strings in strings?

3 ビュー (過去 30 日間)
Billy Jones
Billy Jones 2020 年 5 月 13 日
コメント済み: Billy Jones 2020 年 5 月 13 日
Hello,
I get many strings from our devices with some information. There are two different kinds of strings :
  1. 011_c_srr_Rtzfi_at000_hh5_fs_v343_l067_i1_Test_Test or 041_c_ddr_Rtzfi_ds000_hh5_fs_v343_l037_i1_Test_hall and so on ......
  2. 061_t_err_Rsas_au000_ti3_fs_v777_l011_ or 021_t_err_Rsas_au230_ti3_fs_v777_l031_ and so on ......
I need the following part of the string to get the information i need l067 / l037 / l011 and so on. l067 means 67% and I037 means 37%
So the result i want is 67 / 37 ..... :)
I am new here i spend several hours but my code does not work... :/
Thank you

回答 (3 件)

Antonio Jesús Periñan Freire
Antonio Jesús Periñan Freire 2020 年 5 月 13 日
Hello,
you can do this easily with text processing.
First import the data
source = importdata('matlabtext.txt');
% Define a pattern (you can do this also for the other 2 patterns)
pattern = 'l067';
Then search for this pattern within the imported text file:
% This will return empty when the string pattern is not found:
rows_A = cellfun(@(x)strfind(x,pattern),source,'un',0);
% And convert that into ones and delete the empty values
rows_A = find(~cellfun(@isempty,rows_A));
Then simply get the length of the variable and this will give you the number of times that this string appears in the text:
num_A = length(rows_A)
  3 件のコメント
Antonio Jesús Periñan Freire
Antonio Jesús Periñan Freire 2020 年 5 月 13 日
Is then your pattern _I0xx? or the _I0 string is repeated somewhere else?
You can use that _I0 to find the position in the string you want to extract.
Billy Jones
Billy Jones 2020 年 5 月 13 日
Is then your pattern _I0xx? or the _I0 string is repeated somewhere else? -> _I following with 3 three numbers is not repeating its unique ! Attention: _I100 means 100% after _I is not always 0! so i have to find _I and the the next 3 characters must be a number! Thx

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


Walter Roberson
Walter Roberson 2020 年 5 月 13 日
編集済み: Walter Roberson 2020 年 5 月 13 日
regexp(VARIABLE, '(?<=_l0)\d\d(?=_)', 'match', 'once')
VARIABLE can be a character vector, or a cell array of character vectors, or a string array.
You can str2double() the output

Stephen23
Stephen23 2020 年 5 月 13 日
編集済み: Stephen23 2020 年 5 月 13 日
>> str = '011_c_srr_Rtzfi_at000_hh5_fs_v343_l067_i1_Test_Test';
>> tmp = regexp(str,'(?<=_l)\d+','match','once');
>> num = str2double(tmp)
num =
67
If multiple occurances then without 'once' option.

カテゴリ

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