c# - Find .NET Recognizable Guids in a String -
i looking way find guid within string in .net recognizable pattern.
there few regex's available in popular library, can't find 1 works of possible guids outlined here in msdn article.
for example, lets have string this:
activity: "{0xca761232, 0xed42, 0x11ce, {0xba, 0xcd, 0x00, 0xaa, 0x00, 0x57, 0xb2, 0x23}}", time:09:09:09:09
this should return:
{0xca761232, 0xed42, 0x11ce, {0xba, 0xcd, 0x00, 0xaa, 0x00, 0x57, 0xb2, 0x23}}
another example be:
random string ca761232-ed42-11ce-bacd-00aa0057b223 random string
this should return:
ca761232-ed42-11ce-bacd-00aa0057b223
any ideas on how approach this? regular expressions way go here?
solution is:
using system; using system.text.regularexpressions; class program { static void main() { string input = "random string ca761232-ed42-11ce-bacd-00aa0057b223 random string"; match match = regex.match(input, @"((?:(?:\s*\{*\s*(?:0x[\da-f]+)\}*\,?)+)|(?<![a-f\d])[a-f\d]{32}(?![a-f\d])|" + @"(?:\{\(|)(?<![a-f\d])[a-f\d]{8}(?:\-[a-f\d]{4}){3}\-[a-f\d]{12}(?![a-f\d])(?:\}|\)|))"); if (match.success) { string key = match.groups[1].value; console.writeline(key); } else { console.writeline("no match"); } } }
see , test code here.
Comments
Post a Comment