9

我需要使用在字符串中找到匹配的 GUIDRegex

string findGuid="hi sdkfj 1481de3f-281e-9902-f98b-31e9e422431f sdfsf 1481de3f-281e-9902-f98b-31e9e422431f"
var guid = Regex.Match(m.HtmlBody.TextData, @"^(\{){0,1}[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}(\}){0,1}$").Value;
4

5 回答 5

24

如果您想使用Regex模式获取 GUID。然后,试试这个模式

(\{){0,1}[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}(\}){0,1}

例子

string findGuid = "hi sdkfj 1481de3f-281e-9902-f98b-31e9e422431f sdfsf 1481de3f-281e-9902-f98b-31e9e422431f"; //Initialize a new string value
MatchCollection guids = Regex.Matches(findGuid, @"(\{){0,1}[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}(\}){0,1}"); //Match all substrings in findGuid
for (int i = 0; i < guids.Count; i++)
{
    string Match = guids[i].Value; //Set Match to the value from the match
    MessageBox.Show(Match); //Show the value in a messagebox (Not required)
}

正则表达式匹配器匹配字符串中的两个 GUID

注意:我使用了您提供的相同模式,但只是删除了^指示表达式必须从字符串开头匹配的字符。然后,$从字符串末尾删除表示表达式必须匹配的字符。

更多关于正则表达式的信息可以在这里找到:
正则表达式 - 一个简单的用户指南和教程

谢谢,
我希望你觉得这有帮助:)

于 2012-11-02T06:45:06.373 回答
9

看起来您使用了不正确的正则表达式。如果你需要指导

{8}-{4}-{4}-{4}-{12}

应该像

[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 findGuid="hi sdkfj 1481de3f-281e-9902-f98b-31e9e422431f sdfsf 1481de3f-281e-9902-f98b-31e9e422431f";
    string regexp = @"[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}";
    if (Regex.IsMatch(findGuid, regexp))
    {
        Console.WriteLine(
        Regex.Match(findGuid, regexp).Value
        );

    }
于 2012-11-02T06:45:55.783 回答
2

您可以在拆分数组上使用Guid.TryParse。就像是:

string findGuid = "hi sdkfj 1481de3f-281e-9902-f98b-31e9e422431f sdfsf 1481de3f-281e-9902-f98b-31e9e422431f";
string[] splitArray = findGuid.Split();
List<Guid> listofGuid = new List<Guid>();
foreach (string str in splitArray)
{
    Guid temp;
    if (Guid.TryParse(str, out temp))
        listofGuid.Add(temp);
}

这将为您提供 Guid 列表中的两个项目

编辑:对于根据 OP 评论的新字符串,

string findGuid="hi sdkfj x-Guid:1481de3f-281e-9902-f98b-31e9e422431f sdfsf x-Guid:1481de3f-281e-9902-f98b-31e9e422431f"

可以指定多个拆分分隔符,例如:

string[] splitArray = findGuid.Split(' ', ':');
于 2012-11-02T06:35:28.343 回答
1
模式 = "\b[\dA-F]{8}-[\dA-F]{4}-[\dA-F]{4}-[\dA-F]{4}-[\dA-F ]{12}-\d+"
string input = "name=4a3779ab-56cc-41b5-ac7c-03bbf673439c-53607.jpg This is input string";
    Match m = Regex.Match(input, @"\b[\dA-F]{8}-[\dA-F]{4}-[\dA-F]{4}-[\dA-F]{4}-[\dA-F]{12}", RegexOptions.IgnoreCase);
    string output = null;
    if (m.Success)
        output = m.Value;
于 2015-03-19T12:08:05.457 回答
-1

使用 .NET 4.0 及更高版本,您可以使用 Guid.TryParse。这是我使用的扩展方法。

public static bool IsGuid(this string checkGuid)
{
    if (string.IsNullOrEmpty(checkGuid)) return false;
    Guid resultGuid;
    return Guid.TryParse(checkGuid, out resultGuid);
}

这就是你如何称呼它。

public MyMethod(string myToken)
{
    if (myToken.IsGuid())
    {
        // Do something
    }
}
于 2015-04-16T13:45:31.447 回答