regexp: Extract optional named tokens

3 ビュー (過去 30 日間)
Hau Kit Yong
Hau Kit Yong 2019 年 7 月 2 日
編集済み: Akira Agata 2019 年 7 月 3 日
I would like to extract some information from the following text:
text.PNG
There are 3 groups in the text. I want to extract the genders (enclosed in brackets), the group names (the text following 'Name:') and the student IDs for each group (the numbers following 'ID XX =').
My desired output is as follows:
struct.PNG
The issue is that not all groups have a header line (the lines starting with '#'), e.g. for group 3.
My code is as follows
str = fileread('trip-data.txt');
expr = 'Student group.+?\((?<Gender>\w+?)\).*?Name:(?<Name>.+?)\nGROUP.+?=(?<IDs>.+?(,\s*\n.+?)*)(?=(\n|$))';
groups = regexp(str, expr, 'names');
The returned struct array ignores group 3:
Capture.PNG
I have also tried enclosing the header line in an optional bracket, e.g. '()?', like so
expr = '(Student group.+?\((?<Gender>\w+?)\).*?Name:(?<Name>.+?))?\nGROUP.+?=(?<IDs>.+?(,\s*\n.+?)*)(?=(\n|$))';
The returned struct captures the 'ID' fields but not the 'Gender' and 'Name' fields for all 3 groups:
Capture1.PNG
  2 件のコメント
Rik
Rik 2019 年 7 月 2 日
Do you absolutely need to use a regexp? Because it might be easier with other tools (if maybe slightly less efficient).
Hau Kit Yong
Hau Kit Yong 2019 年 7 月 2 日
I would like to, yes, because the text is a small snippet of a much larger file with varying formats that I am already parsing with other expressions.

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

回答 (1 件)

Akira Agata
Akira Agata 2019 年 7 月 3 日
編集済み: Akira Agata 2019 年 7 月 3 日
How about extracting 'Name', 'Gender' and 'ID' one-by-one?
The following is an example.
% Read the file
str = fileread('trip-data.txt');
% Remove newline in ID
str = regexprep(str,'\r\n\s+','');
% Remove newline after 'Name: XX'
str = regexprep(str,'(Name:\s+\w+)\r\n','$1, ');
% Store each line as a cell array
c = strsplit(str,'\r\n')';
% Extract one-by-one
Name = erase(regexp(c,'Name:\s(\w+)','match','once'),'Name: ');
Gender = regexp(c,'(male|female)','match','once');
ID = strtrim(extractAfter(c,'='));
% Summarize as a table
tbl = table(Name,Gender,ID);

カテゴリ

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