Define regularExpression for Strsplit?

11 ビュー (過去 30 日間)
FishermanJack
FishermanJack 2017 年 11 月 23 日
コメント済み: FishermanJack 2017 年 11 月 23 日
Hi, i have a text file consisting of multiple headers with Data in between. Now, i want to use Strsplit to find the sequence of the headers and save them under a Variable.
The Problem is the all Headers are different from each other, instead of the first Word and letter.
ex.
  1. abc 0 AAA BB CC DDD 111
  2. abc 0 EEE FF GGG HH II 120
1. and 2. are not the Content of the Header. so the abc and 0 are tab delimited. after 0 there is no common sequence for the line, some of the words are tab delimited some with space, some have numbers some not.
The ending of the File can be 'just' one of These, because it has more thousands headers.
110, 005, 006, 010, 133/1A, 230, 400, NWD
The Expression i started Looks like this:
xpr = '(?m-s)^abc\s+';
but how to define the ending?
Anyone to suggest something?

採用された回答

per isakson
per isakson 2017 年 11 月 23 日
編集済み: per isakson 2017 年 11 月 23 日
I've understood the question differently
str = fileread( ... );
[ data_blocks, headers ] = strsplit( str, '(?m-s)^abc\t0.+$', 'DelimiterType','RegularExpression' )
  5 件のコメント
per isakson
per isakson 2017 年 11 月 23 日
I didn't fully understood the questions and thus I made some assumptions:
  1. The entire lines shown in the question are headers. The blocks of data between the headers are not shown in the question.
  2. Every line starting with abc\t0 is a header. ( \t for tab )
  3. I still don't understand the role of the string NWD. Does it indicate the last header of the file? I ignored it.
FishermanJack
FishermanJack 2017 年 11 月 23 日
1. Yes 2. Yes 3. No These are some random letters that are at the end of some Headers, like 110 or 005

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 11 月 23 日
S = fileread('YourFile.txt');
regexp(S, '^abc\s.*110,\s+005,\s+006,\s+010,\s+133/1A,\s+230,\s+400,\s+NWD', 'match', 'lineanchors')
If all of the headers are the same and there is nothing between the 110* line and the next header, then consider
regexp(S, '^abc\s', 'split', 'lineanchors')
The 'abc' will be removed from each block during the splitting; it would be possible to get around that but doing so is somewhat more obscure.

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by