【问题标题】:Break the string using Regex C#使用正则表达式 C# 断开字符串
【发布时间】:2016-05-17 04:29:51
【问题描述】:
var str =  "AB: Pillow D-001-134552";

我想使用正则表达式将上面的字符串分解为[D-001-134552]。(起始位置 D 只占用一个字符,下一个 001 是 3 个字符,但最后一个路径也可以采用单个数字或 7 位数字)。

【问题讨论】:

标签: .net regex string c#-4.0 break


【解决方案1】:

由于您只需要 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;

【讨论】:

    猜你喜欢
    • 2011-09-16
    • 1970-01-01
    • 2012-08-05
    • 2010-11-02
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    相关资源
    最近更新 更多