Regex to get a guid from a email reply

强颜欢笑 提交于 2019-11-27 03:16:13

问题


Trying to figure out the Regex pattern to match if an email contains a Guid, e.g.

a141aa94-3bec-4b68-b562-6b05fc2bfa48-reply@site.com

The Guid could potentially be anywhere before the @, e.g.

reply-a141aa94-3bec-4b68-b562-6b05fc2bfa48@wingertdesign.com


回答1:


I use this to find Guids

Regex isGuid = new Regex(@"^(\{){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}$", RegexOptions.Compiled);



回答2:


A lazy variant would be

([0-9a-f-]{36}).*?@

It is easy to read and I bet it matches 99,99% of all cases ;) But then in 0,00001% of all cases sombody could have an email address that fits in a GUID scheme.




回答3:


Well, assuming it's always going to be in standard GUID notation like that, if the following regex matches there was a GUID. You should also apply your language's method of making it case-insensitive.

[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}[^@]*@



回答4:


^[^@]*([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})

will match the any hex in the 8-4-4-4-12 format that comes before a @




回答5:


there is also one way in a single line to get GUID

 string findGuid = "hi Aether experiment 1481de3f-281e-9902-f98b-31e9e422431f @sdfsf 1481de3f-281e-9902-f98b-31e9e422431f"; //Initialize a new string value
 var guids = Regex.Matches(Regex.Split(findGuid, "@")[0], @"(\{){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}")[0]; //Match all substrings in findGuid


来源:https://stackoverflow.com/questions/856327/regex-to-get-a-guid-from-a-email-reply

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!