Extract serial number from string by using regex in c# -
serial number :- 4540107708_e4-ur730rsq_xicm_1
code (c#):-
string pattern = "^[0-9]{1,10}_[a-z0-9a-z-]{2,12}_xicm_[0-9]$"; string inputstr = "abcd abcdefg abcdefghij xyzyzxue, 4540107708_e4-ur730rsq_xicm_1 abcdefg abcd abcdefg abcdefghij xyzyzxue."; regex regex = new regex(pattern,regexoptions.none); match match = regex.match(inputstr); if (match.success) { string matchvalue = match.value; console.writeline(matchvalue); console.writeline(match.value); console.readline(); }
i want serial number (4540107708_e4-ur730rsq_xicm_1) inputstr.
please help..
the ^
anchor requires match appear @ start of string , $
requires match appear @ end of string. need find match whole word. use word boundaries:
\b[0-9]{1,10}_[a-z0-9a-z-]{2,12}_xicm_[0-9]\b ^^ ^^
see regex demo
Comments
Post a Comment