David Green David Green - 1 month ago 5
C# Question

Need RegEx Pattern To Match '--'

Here's some input

36 1 36 0ef6c0 id N8 Online FC F-Port 50:05:07:68:01:80:fc:ae
37 1 37 0ef680 id N8 Online FC F-Port 50:05:07:68:01:80:fa:1b
38 1 38 0ef640 id N8 Online FC F-Port 50:05:07:68:01:80:fa:2e
39 1 39 0ef600 id N8 Online FC F-Port 50:05:07:68:01:80:f9:ca
86 2 22 0eea40 -- -- Online VE VE-Port 10:00:00:05:33:ea:da:4d "replsa2v" (downstream)
87 2 23 0eea00 -- -- Offline VE Disabled (Persistent)
88 2 24 0ee9c0 -- -- Offline VE Disabled (Persistent)


This regular expression matches the first 4 lines.

(?<index>\d+)\s+(?<slot>\d+)\s+(?<port>\d+)\s+(?<address>\w{6,})\s+\b(id|cu)\b\s+(?<speed>\w\d+)\s+\b(?<state>\w+)\s+(?<protocol>\S+)\s+(?<type>\S+)\s(?<extra>.*)


This one matches the last 3

(?<index>\d+)\s+(?<slot>\d+)\s+(?<port>\d+)\s+(?<address>\w{6,})\s+[-]{2}\s+[-]{2}\s+\b(?<state>\w+)\s+(?<protocol>\S+)\s+(?<type>\S+)\s(?<extra>.*)


I'd like one pattern that matches all. The code works but it would be cleaner and slightly faster if I was doing a single match. The best I can do with a single pattern is to match the first half of the first 4 lines of input and the last half of the last 3 - basically everything before the first '--' and after the last one.

Answer

this work for me:

(?<index>\d+)\s+
(?<slot>\d+)\s+
(?<port>\d+)\s+
(?<address>\w{6,})\s+
(id|cu|-{2})\s+
(?<speed>\w\d+|-{2})\s+
\b(?<state>\w+)\s+
(?<protocol>\S+)\s+
(?<type>\S+)\s
(?<extra>.*)
Comments