c# - Extracting a number from between parenthesis -
i have regular expression designed extract number between 2 parenthesis. had been working fine until made input string customizable. now, if number found somewhere else in string, last number taken. expression below:
int icorrespid = convert.toint32(regex.match(subject, @"(\d+)(?!.#\d)", regexoptions.righttoleft).value);
if send string this (12) test
, works fine, extracting 12. however, if send this (12) test2
, result 2. realize can change righttoleft lefttoright, fix instance, want number between parenthesis.
i sure easy regular expression experience (which not me). hoping show me how correct want, give brief explanation of doing wrong can improve.
thank you.
additional information
i appreciate of responses. have taken agreed upon advice, , tried each of these formats:
int icorrespid = convert.toint32(regex.match(subject, @"(\(\d+\))(?!.#\d)", regexoptions.righttoleft).value); int icorrespid = convert.toint32(regex.match(subject, @"(\(\d+\))", regexoptions.righttoleft).value); int icorrespid = convert.toint32(regex.match(subject, @"\(\d+\)", regexoptions.righttoleft).value);
unfortunately, each, exception stating input string not in correct format. did not before. i'm sure resolve without using regular expression in minute or two, stubbornness has kicked in.
thank comments.
try regular expression:
\(#(\d+)\)
the brackets escaped \(
, \)
, inside them normal search numbers.
if use .value
property, give number surrounded brackets. instead need use groups
collection. use in code, this: (now added error checking!)
var match = regex.match("hgf", @"\(#(\d+)\)", regexoptions.righttoleft).groups[1].value; if(!string.isnullorempty(match)) { var icorrespid = convert.toint32(match); } else { //no match found }
Comments
Post a Comment