使用正则表达式 C# 断开字符串
Break the string using Regex C#
var str = "AB: Pillow D-001-134552";
我想用正则表达式将上面的字符串分解为[D-001-134552]
。(起始位置D只占一个字符,接下来的001是3个字符,但最后一个路径也可以是单个数字或7位数字)。
由于您只需要 ID 字符串,因此在这种情况下无需使用捕获组。
所以一行就够了。
下面的解决方案假定 id 具有特定格式:
1 个字符,破折号,3 位数字,破折号,6 到 7 位数字。
string str = "AB: Pillow D-001-134552";
var id = System.Text.RegularExpressions.Regex.Match(str, @"\w-\d{3}-\d{6,7}").Value;
var str = "AB: Pillow D-001-134552";
我想用正则表达式将上面的字符串分解为[D-001-134552]
。(起始位置D只占一个字符,接下来的001是3个字符,但最后一个路径也可以是单个数字或7位数字)。
由于您只需要 ID 字符串,因此在这种情况下无需使用捕获组。
所以一行就够了。
下面的解决方案假定 id 具有特定格式:
1 个字符,破折号,3 位数字,破折号,6 到 7 位数字。
string str = "AB: Pillow D-001-134552";
var id = System.Text.RegularExpressions.Regex.Match(str, @"\w-\d{3}-\d{6,7}").Value;