Define regularExpression for Strsplit?

4 views (last 30 days)
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?

Accepted Answer

per isakson
per isakson on 23 Nov 2017
Edited: per isakson on 23 Nov 2017
I've understood the question differently
str = fileread( ... );
[ data_blocks, headers ] = strsplit( str, '(?m-s)^abc\t0.+$', 'DelimiterType','RegularExpression' )
  5 Comments
per isakson
per isakson on 23 Nov 2017
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 on 23 Nov 2017
1. Yes 2. Yes 3. No These are some random letters that are at the end of some Headers, like 110 or 005

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 23 Nov 2017
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.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!