I am trying to extract values in groups from a string. My regex is
string str = @"DEMOV 1'07"" MOT Lifestyle 503080 Pure Rain Nozzle Feb 13 472000";
const string type = @"(?<type>\w+)";
const string minutes = @"((?<minutes>\d+)\')?";
const string seconds = @"((?<seconds>\d+)\"")?";
const string body = @"(?<body>.+)";
const string id = @"(?<id>\s\d{6})?";
var pattern1 = String.Format(@"^{0}(?:\s\w+)?\s({1}{2}|{1}|{2})\s?{3}{4}$", type, minutes, seconds, body, id);
var m1 = Regex.Match(str, pattern1);
I am getting the match, but the the group is not getting the last 5 digit.
Can anyone tell me what i am doing wrong here?
Please find below the output i am getting.
Use the non-greedy version (use .+?
for body
), and don't add the spaces to the id
group:
const string body = @"(?<body>.+?)";
const string id = @"\s(?<id>\d{6})?";
In action: https://dotnetfiddle.net/L1yL3C
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments