You can just define a variable repetition by passing 2 values within {}
, e.g. \d{1,3}
would match 1-3 digits.
But overall, I'd probably use less regular expressions for this, given the format is rather nice to parse:
- First of all I'd loop through all lines as an array of strings.
- If one line is empty, ignore it, if two consecutive lines are empty, a new dataset is created.
- If the line includes a
:
you split the line there: the left part becomes the key, the right part the value. - If the line doesn't include a
:
it's either ignored or an error flag raised (or exception thrown or whatever). - You can then use a simple string match (faster than a regular expression) to determine the meaning of the "key".
- Another regular expression (or just something like
Double.Parse()
) then extracts the value from the right. Keep in mind that these conversion functions will simply skip invalid characters and drop anything trailing them. - The more complex entries (like coordinates or time stamp; or the entries where the unit is important) can then be parsed using a simple regular expression.
Simplified code (not necessarily 100% compileable/correct):
String[] lines = fileContents.Split({'\n'}); // split the content into separate lines
bool wasEmpty = false;
foreach (String line in lines) {
line = line.Trim(); // remove leading/trailing whitespaces
if (line.Length == 0) { // line is empty
if (wasEmpty) { // last line was empty, too
// init a new dataset
}
else
wasEmpty = true;
continue; // skip to next entry
}
wasEmpty = false;
String content = line.split({':'}); // split the line into a key/value pair
if (content.Length != 2) // not exactly two entries
continue; // skip
// content[0] now has the "key" (like "Lat/Long")
// content[1] now has the "value" (like "18.38891, -66.12175")
// both can be evaluated using regular expressions
}