c# - Use Regex in this Linq query? -
i using linq find highest int in list, can increment , add end of next string:
var cablenumber = clist.select(v => int.parse(v.cablenumber.substring(n))).max();
however, because strings aren't fixed length, thinking of inserting regex.match
in there, possibly like:
n = regex.match(cablenumber, @"\d{3}", regexoptions.righttoleft);
to specify; format input strings follow have 3 digit number on end, possibly followed single letter. examples:
cp1-p-cp2-001 (001) mot1psp2023a (023) tksp3-c-flt2-234-a (234)
how implement this? there better way?
the following uses regex pattern inside linq query:
string[] strings = { "cp1-p-cp2-001 (001)","mot1psp2023a (023)", "tksp3-c-flt2-234-a (234)", "invalidstring" }; int? maxvalue = strings.max(x => { var match = regex.match(x, @"\d{3}(?=\d*$)"); return match.success ? (int?) int.parse(match.value) : null; });
the int?
can bypass string.empty
coming invalid match , parse valid matches. if none matched, return null.
Comments
Post a Comment