简体   繁体   English

通过在C#中使用正则表达式从字符串中提取序列号

[英]Extract serial number from string by using regex in c#

Serial Number :- 4540107708_E4-UR730RSQ_XICM_1 序列号: -4540107708_E4-UR730RSQ_XICM_1

Code (C#):- 代码(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 this serial number (4540107708_E4-UR730RSQ_XICM_1) from inputStr. 我想从inputStr获得此序列号(4540107708_E4-UR730RSQ_XICM_1)。

please help.. 请帮忙..

output 产量

The ^ anchor requires the match to appear at the start of the string and $ requires the match to appear at the end of the string. ^锚要求匹配项出现在字符串的开头, $要求匹配项出现在字符串的末尾。 What you need is to find the match as a whole word. 您需要的是找到一个完整的匹配词。 Use word boundaries: 使用单词边界:

\b[0-9]{1,10}_[a-z0-9A-Z-]{2,12}_XICM_[0-9]\b
^^                                         ^^

See the regex demo 正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM