take out numbers from string

1 回表示 (過去 30 日間)
Jay Hanuman
Jay Hanuman 2016 年 12 月 19 日
コメント済み: Jay Hanuman 2016 年 12 月 19 日
I attached file which contains as this
'10.0.7.4:22->10.0.8.5:26856'
'10.0.13.4:22->10.0.12.5:9997'
'10.0.9.4:22->10.0.10.5:47576'
'10.0.14.4:443->10.0.10.5:4922'
'10.0.14.4:443->10.0.10.5:20113'
'10.0.11.4:80->10.0.13.5:61779'
'10.0.10.4:22->10.0.9.5:64788'
'10.0.9.4:80->10.0.10.5:25283'
I want to take out numbers appears after 1st and 2nd colon in two different variable. i.e. A=[22 22 443 443 80 22 80] and B=[26856 9997 47576 4922....] but A and B should be in column format. how to do it.

採用された回答

Guillaume
Guillaume 2016 年 12 月 19 日
There's no point in creating two different variables when it's much easier to have the output as two columns of the same variable. Here's one way to do it:
suffix = regexp(VarName6, '(?<=:)\d+', 'match'); %match sequence of numbers following a :
suffix = str2double(vertcat(suffix{:})) %convert cell array of cell arrays into one big cell array and convert strings to numeric
  2 件のコメント
José-Luis
José-Luis 2016 年 12 月 19 日
編集済み: José-Luis 2016 年 12 月 19 日
I really should work on my regexes... +1
Jay Hanuman
Jay Hanuman 2016 年 12 月 19 日
works Thank you.

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

その他の回答 (1 件)

José-Luis
José-Luis 2016 年 12 月 19 日
編集済み: José-Luis 2016 年 12 月 19 日
I'll get you started:
data = [{'10.0.7.4:22->10.0.8.5:26856'};{'10.0.13.4:22->10.0.12.5:9997'}];
result = cellfun(@(x) regexp(x,':[0-9]+','match'),data,'UniformOutput',false);
a = cellfun(@(x) sscanf(x{1},':%d'),result);
b = cellfun(@(x) sscanf(x{2},':%d'),result);
There are other, possibly better, ways.
  2 件のコメント
Guillaume
Guillaume 2016 年 12 月 19 日
編集済み: Guillaume 2016 年 12 月 19 日
Well, I'd certainly use captures (tokens in matlab) or look-behind to avoid returning the : as part of the match.
Capture:
regexp(x, ':([0-9]+)', 'tokens')
Look-behind:
regexp(x, '(?<=:)[0-9]+', 'match')
Jay Hanuman
Jay Hanuman 2016 年 12 月 19 日
works Thank you.

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

カテゴリ

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