Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在字符串中查找匹配的guid_C#_Regex - Fatal编程技术网

C# 在字符串中查找匹配的guid

C# 在字符串中查找匹配的guid,c#,regex,C#,Regex,我需要使用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]{

我需要使用
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;
您可以在拆分的阵列上使用。比如:

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);
}
string[] splitArray = findGuid.Split(' ', ':');
可以指定多个拆分分隔符,例如:

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);
}
string[] splitArray = findGuid.Split(' ', ':');

如果要使用
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)
}
string findGuid=“hi sdkfj 1481de3f-281e-9902-f98b-31E9E42431F sdfsf 1481de3f-281e-9902-f98b-31E9E42431F”//初始化一个新的字符串值
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}”)//匹配findGuid中的所有子字符串
for(int i=0;i

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

有关正则表达式的更多信息,请参见:

谢谢,

我希望这对您有所帮助:)

看起来您使用的正则表达式不正确。 如果您需要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
        );

    }
Pattern=“\b[\dA-F]{8}-[\dA-F]{4}-[\dA-F]{4}-[\dA-F]{4}-[\dA-F]{12}-\d+”
在.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
    }
}

你的代码不管用吗?问题是什么?我无法获得匹配的图形我不同意这不是重复的这实际上不是100%重复的,但是,对于标记为重复的问题,您当然可以使用顶部答案,并对其进行调整,以删除表示表达式必须从字符串开头匹配的“^”字符和表示表达式必须从字符串结尾匹配的$字符。然后,您可以将(?i)添加到不区分大小写的开始信号中,这样就不需要使用RegexOptions.IgnoreCase,然后您将得到:(?i)[{(]?[0-9A-F]{8}[-]?([0-9A-F]{4}[-]){3}[0-9A-F]{12}[])?没有参数的Split在字符串上做什么?@nawfal,在空间上拆分它,刚刚了解到如果字符串像这个字符串findGuid=“嗨,sdkfj x-Guid:1481de3f-281e-9902-f98b-31E9E42431F sdfsf x-Guid:1481de3f-281e-9902-f98b-31E9E42431F”@Habib哇..很高兴知道。谢谢。@user1551433,在这种情况下,您必须对多个字符进行拆分:
string[]splitArray=findGuid.split('':');