Define regularExpression for Strsplit?
4 views (last 30 days)
Show older comments
FishermanJack
on 23 Nov 2017
Commented: FishermanJack
on 23 Nov 2017
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.
- abc 0 AAA BB CC DDD 111
- 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?
0 Comments
Accepted Answer
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
on 23 Nov 2017
I didn't fully understood the questions and thus I made some assumptions:
- The entire lines shown in the question are headers. The blocks of data between the headers are not shown in the question.
- Every line starting with abc\t0 is a header. ( \t for tab )
- I still don't understand the role of the string NWD. Does it indicate the last header of the file? I ignored it.
More Answers (1)
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.
0 Comments
See Also
Categories
Find more on Data Import and Export 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!