Using regexp to Search Large Text File for Wanted Data

5 Ansichten (letzte 30 Tage)
Zachary  Parra
Zachary Parra am 5 Okt. 2017
Bearbeitet: Cedric am 7 Okt. 2017
Hello all! I am attempting to utilize regexp to extract wanted items from text files. Attached below is a sample text file. I wish to extract the start date, EL HGT and Northing (Y)/Easting (X) for UTM (Zone 15). I will eventually have a series of 365 of these text files compressed into one file. Is regexp the best method and how would it be coded? Thanks.

Akzeptierte Antwort

Guillaume
Guillaume am 5 Okt. 2017
is regexp the best method?
For EL HGT, probably. For your Northing/Easting for UTM(Zone 15), no because extracting that value involve crossreferencing rows and columns. For that you would have to parse the whole file, and you would have to write your own parser as none of matlab built-in parsers (textscan, etc.) can parse a file that complex as is.
However, if UTM(Zone 15) is always the first column of number for Northing/Easting, then yes you could use a regexp:
filecontent = fileread('sample.txt');
el_hgt_full = regexp(filecontent, '(?<=EL HGT:\s*)[^\n\r]*', 'match', 'once');
el_hgt = str2double(regexp(el_hgt_full, '[+-]?\d+(\.\d+)?', 'match'));
northing = str2double(regexp(filecontent, '(?<=Northing \(Y\) \[meters\]\s*)[+-]?\d+(\.\d+)?', 'match', 'once'));
easting = str2double(regexp(filecontent, '(?<=Easting \(X\) \[meters\]\s*)[+-]?\d+(\.\d+)?', 'match', 'once'));
  6 Kommentare
Zachary  Parra
Zachary Parra am 7 Okt. 2017
Thank you for the comprehensive answer. This will be extremely helpful to look back to in the future.
Cedric
Cedric am 7 Okt. 2017
Bearbeitet: Cedric am 7 Okt. 2017
My pleasure!
(Last edit @ 21:43 UTC)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by