How to extract info from string using regexp?

3 ビュー (過去 30 日間)
Peter Valent
Peter Valent 2019 年 5 月 22 日
コメント済み: Peter Valent 2019 年 5 月 22 日
I have a text file with a number of lines such as:
[DEBUG][HoraCorrelationMatrixNode] Polygon 2303 id 857befored
[DEBUG][HoraCorrelationMatrixNode] Polygon 2304 id 88befored
[DEBUG][HoraCorrelationMatrixNode] Polygon 2305 id 930befored
[DEBUG][HoraCorrelationMatrixNode] Polygon 2306 id 1000d
[DEBUG][HoraCorrelationMatrixNode] Polygon 2307 id 1001d
I need to extract both polygon number (2303) and its id (857befored). How to write a regular expression pattern to obtain both information?
Thanks
  1 件のコメント
Peter Valent
Peter Valent 2019 年 5 月 22 日
@Stephen Cobeldick: It has to be regular expression beccause the txt file also contains line that are formated diferently. I Think that a regular expression is the best solution for this task. I just didn't know how to write the patterns. But thank you for suggestions.

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

採用された回答

Guillaume
Guillaume 2019 年 5 月 22 日
This should work:
filecontent = fileread('c:\somewhere\somefile');
polyid = regexp(filecontent, 'Polygon (\d+) id (\S+)', 'tokens');
polyid = vertcat(polyid{:});
result = table(str2double(polyid(:, 1)), polyid(:, 2), 'VariableNames', {'Polygon', 'ID'})
  1 件のコメント
Peter Valent
Peter Valent 2019 年 5 月 22 日
Works very well. Thanks.

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

その他の回答 (1 件)

Raghunandan V
Raghunandan V 2019 年 5 月 22 日
Hi,
Since all the lines are of same format. I would reccomend you to do something like this
str = ['[DEBUG][HoraCorrelationMatrixNode] Polygon 2303 id 857befored']
Polygon_num = str(45:49)
id_num = str(52:end)
This would be easier to implement

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by