簡體   English   中英

通過在C#中使用正則表達式從字符串中提取序列號

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

序列號: -4540107708_E4-UR730RSQ_XICM_1

代碼(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();
}

我想從inputStr獲得此序列號(4540107708_E4-UR730RSQ_XICM_1)。

請幫忙..

產量

^錨要求匹配項出現在字符串的開頭, $要求匹配項出現在字符串的末尾。 您需要的是找到一個完整的匹配詞。 使用單詞邊界:

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

正則表達式演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM