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..

output

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

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -