I am presented with a data file and in the file I have a line I need to match, then I'll read the next few lines until I find this match again.
Here are the various structures of the lines I need to match:
69 1.0 PKS-EKC-FlYCTRK--Y 2,110.28 2,110.28
70 2.0 ACS-PMM 31.75 63.50
72 1.0 PKS-TR1-CRD 308.14 308.14
73 1.0 QTC-01HZZ-RBER058- 1,725.57 1,725.57
74 1.0 MGS-05B-4TC-120--8 1,222.84 1,222.84
75 1.0 ACS-VGY 98.60 98.60
76 2.0 ACS-VGG 27.27 54.53
77 2.0 ACS-VGQ 110.93 221.86
78 2.0 ECS-ENM--845 1,294.18 2,588.36
80 1.0 FREIGHT 4,188.00 4,188.00
Here is what I have thought of so far but it only matches one line, I need to match them all.
(\d{2})\s(\d{1}.\d{1})\s(\w ?-\w ?-\w ?--\w)\s(\d ?,\d .\d )\s(\d ?,\d .\d )
CodePudding user response:
You could examine the lines like this:
bool IsThisIt(string line)
{
var parts = line.Split(' ');
if(parts.Length != 5) return false;
// Number
if(!int.TryParse(parts[0], out var _)) return false;
// Type
if(!decimal.TryParse(parts[1], out var _)) return false;
// Price 1
if(!decimal.TryParse(parts[3], out var _)) return false;
// Price 2
if(!decimal.TryParse(parts[4], out var _)) return false;
return true;
}
CodePudding user response:
I love a good regex problem. Solved!
^(\d{2})\s(\d\.\d)\s(\S*)\s((\d*\,\d*.\d{2})|(\d*.\d{2}))\s((\d*\,\d*.\d{2})|(\d*.\d{2}))$
There are always two digits at the start:
^(\d{2})
Followed by a number with one decimal place:
(\d\.\d)
Followed by some string of non-space characters:
(\S*)
Using OR to deal with different numeric structures:
((\d*\,\d*.\d{2})|(\d*.\d{2}))
regex101.com is your friend.