2012-11-02 157 views
3

匹配的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; 
+1

您的代碼不工作嗎?問題是哪個? – Marco

+0

我無法獲得匹配的GUID – user1551433

+0

http://stackoverflow.com/questions/856327/regex-to-get-a-guid-from-a-email-reply –

回答

7

如果您想使用獲取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}(\} 

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) 
} 

Regex Matcher matching TWO GUIDs from a string

注意:我用您所提供的相同的模式,但簡單地刪除^符,表示該表達式必須匹配從字符串的開頭。然後,刪除$字符,指示表達式必須匹配字符串的末尾。

有關正則表達式的更多信息可以在這裏找到:
Regular Expressions - a Simple User Guide and Tutorial

謝謝,
我希望對您有所幫助:)

1

找到字符串匹配的GUID您可能劈裂陣列上使用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(' ', ':'); 
+0

什麼拆分()沒有參數做一個字符串? – nawfal

+2

@nawfal,將它分割在一個空間,剛學過它[幾天前在SO](http://stackoverflow.com/a/13139223/961113) – Habib

+1

如果字符串是這樣的字符串findGuid =「hi sdkfj x- Guid:1481de3f-281e-9902-f98b-31e9e422431f sdfsf x-Guid:1481de3f-281e-9902-f98b-31e9e422431f「 – user1551433

6

看起來你使用不正確的正則表達式。 如果您需要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} - [O- 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 
     ); 

    } 
1
Pattern =「\ 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; 
0

對於.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 
    } 
}