2012-11-02 159 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 
    } 
}