Reputation: 329
I want to split a text line which has the following pattern:
Name : Type := start_value ; // comment
start_value
and comment
are optional, the semicolon is mandatory. This is an example of a few lines:
MinF : DINT ; //Minimum frequentie: 0 = 0,0 Hz
MaxF : DINT := L#100000; //Maximum reference:75000 = 75,0Hz
RampUp : DINT := L#50; //Ramp up tijd: 100 = 1,00 seconde
RampDown : DINT := L#50; //Ramp down tijd: 100 = 1,00 seconde
V1 : DINT ; //Snelheid 1: 50,00%
Can I do this with just use one regex?
Upvotes: 0
Views: 42
Reputation: 186678
I suggset matching instead of splitting. With a help of pattern
like
string pattern =
@"^\s*(?<Name>[A-Za-z0-9]+)\s*:\s*(?<Type>[A-Za-z0-9]+)(\s*:=\s*(?<Value>\S*))?\s*;\s*(//(?<Comment>.*))?$";
we can easily parse the lines
string[] source = new string[] {
"MinF : DINT ; //Minimum frequentie: 0 = 0,0 Hz",
"MaxF : DINT := L#100000; //Maximum reference:75000 = 75,0Hz",
"RampUp : DINT := L#50; //Ramp up tijd: 100 = 1,00 seconde",
"RampDown : DINT := L#50; //Ramp down tijd: 100 = 1,00 seconde",
"V1 : DINT ; //Snelheid 1: 50,00%",
};
var result = source
.Select(line => Regex.Match(line, pattern))
.Where(match => match.Success)
.Select(match => new {
name = match.Groups["Name"].Value,
type = match.Groups["Type"].Value,
value = match.Groups["Value"].Value,
comment = match.Groups["Comment"].Value, })
.ToArray();
Test
Console.Write(string.Join(Environment.NewLine, result
.Select(item => $"{item.name,-8} {item.type,4} {item.value,-8} {item.comment}")));
Outcome:
MinF DINT Minimum frequentie: 0 = 0,0 Hz
MaxF DINT L#100000 Maximum reference:75000 = 75,0Hz
RampUp DINT L#50 Ramp up tijd: 100 = 1,00 seconde
RampDown DINT L#50 Ramp down tijd: 100 = 1,00 seconde
V1 DINT Snelheid 1: 50,00%
Upvotes: 1