9

を使用して文字列内で一致する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;
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)
}

文字列からの2つのGUIDに一致する正規表現マッチャー

注意:私はあなたが提供したのと同じパターンを使用しましたが^、式が文字列の先頭から一致しなければならないことを示す文字を単に削除しました。$次に、式が文字列の末尾から一致する必要があることを示す文字を削除しました。

正規表現の詳細については、次を参照してください。
正規表現-簡単なユーザーガイドとチュートリアル

ありがとう、
これがお役に立てば幸いです:)

于 2012-11-02T06:45:06.373 に答える
9

間違った正規表現を使用しているようです。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
        );

    }
于 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のリストに2つのアイテムが表示されます

編集: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 に答える