I got some text scraps with this structure
"Focal Plane: 198' Active Aid to Navigation: Yes *Latitude: 35.250 N *Longitude: -75.529 W"
But some of them lack of parts like this
"Focal Plane: 198' Active Aid to Navigation: Yes *Longitude: -75.529 W"
Using regular expression matching, I want to extract all the info available, included getting a tag "NotAvailable" for missing data.,
I wrote this code, but it can't tell when a pattern does not match -- should the pattern in the middle not match, my code just skips it and goes on to the next pattern:
ptrFocal = "(?<=\\Focal Plane: )(.*?)(?=\\')";
ptrLat = "(?<=\\*Latitude: )(.*?)(?=[a-zA-Z])";
ptrLon = "(?<=\\*Longitude: )(.*?)(?=[a-zA-Z])";
r =
StringTrim /@ StringCases[text,
RegularExpression[ptrFocal <> "|" <> ptrLat <> "|" <> ptrLon]
];
I would want some pointers on how to do this.
EDIT:
The expected transformation of the example data I gave above is
{198, 32.250, -75.529} {198, "NotAvailable", -75.529}
W, N, S E
? $\endgroup$ – Kuba♦ May 4 '14 at 8:23StringPattern`PatternConvert[XXX] // InputForm
where XXX is the Mathematica string pattern to see the regex it gets converted to, just FYI. Might give you some ideas on how to build your own if regex is mandatory. $\endgroup$ – ciao May 4 '14 at 8:59