【问题标题】:Regular Expression to identify a Guid followed by a number正则表达式来识别一个 Guid 后跟一个数字
【发布时间】:2015-03-19 15:35:15
【问题描述】:

我有一个可以识别 Guid 的正则表达式。

string pattern = @"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"

我有一个正则表达式可以识别数字

string pattern = "^[0-9]+$"

我需要结合这两个正则表达式。 我的任务是从句子中识别以下类型的字符串。

  Input : id=1 name=4a3779ab-56cc-41b5-ac7c-03bbf673439c-53607.jpg count=53607
  Output : 4a3779ab-56cc-41b5-ac7c-03bbf673439c-53607.jpg 
   or just 4a3779ab-56cc-41b5-ac7c-03bbf673439c-53607

输出是一个 Guid,后跟一个破折号('-')和一个数字。

我怎样才能得到这种正则表达式?

【问题讨论】:

    标签: c# regex string


    【解决方案1】:

    您可以使用以下正则表达式:

    \b[\dA-F]{8}-[\dA-F]{4}-[\dA-F]{4}-[\dA-F]{4}-[\dA-F]{12}-\d+
    

    Demo

    string input = "id=1 name=4a3779ab-56cc-41b5-ac7c-03bbf673439c-53607.jpg count=53607";
    Match m = Regex.Match(input, @"\b[\dA-F]{8}-[\dA-F]{4}-[\dA-F]{4}-[\dA-F]{4}-[\dA-F]{12}-\d+", RegexOptions.IgnoreCase);
    string output = null;
    if (m.Success)
        output = m.Value;
    

    【讨论】:

    • @Umirov - 如果我的输入将包含多个 Guid 怎么办?我假设输入中可以有多个 Guid,但“Guid-Number”的组合将只有一个
    • @TBAG 抱歉,误解了您的问题。我更新了答案。它匹配guid-number 模式。
    猜你喜欢
    • 2018-09-30
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多