在字符串中查找匹配的guid

use*_*433 4 c# regex

我需要在string中找到匹配的GUID Regex

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;
Run Code Online (Sandbox Code Playgroud)

Pic*_*are 14

如果您想使用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}(\}
Run Code Online (Sandbox Code Playgroud)

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)
}
Run Code Online (Sandbox Code Playgroud)

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

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

有关正则表达式的更多信息,请参见此处:
正则表达式 - 简单用户指南和教程

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


Rom*_*nyk 7

看起来你使用不正确的正则表达式.如果你需要guid

{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
        );

    }
Run Code Online (Sandbox Code Playgroud)