C# Regex for Guid

后端 未结 6 1241
情歌与酒
情歌与酒 2020-11-27 03:08

I need to parse through a string and add single quotes around each Guid value. I was thinking I could use a Regex to do this but I\'m not exactly a Regex guru.

Is

6条回答
  •  -上瘾入骨i
    2020-11-27 03:44

    For C# .Net to find and replace any guid looking string from the given text,

    Use this RegEx:

    [({]?[a-fA-F0-9]{8}[-]?([a-fA-F0-9]{4}[-]?){3}[a-fA-F0-9]{12}[})]?
    

    Example C# code:

    var result = Regex.Replace(
          source, 
          @"[({]?[a-fA-F0-9]{8}[-]?([a-fA-F0-9]{4}[-]?){3}[a-fA-F0-9]{12}[})]?", 
          @"${ __UUID}", 
          RegexOptions.IgnoreCase
    );
    

    Surely works! And it matches & replaces the following styles, which are all equivalent and acceptable formats for a GUID.

    "aa761232bd4211cfaacd00aa0057b243" 
    "AA761232-BD42-11CF-AACD-00AA0057B243" 
    "{AA761232-BD42-11CF-AACD-00AA0057B243}" 
    "(AA761232-BD42-11CF-AACD-00AA0057B243)" 
    

提交回复
热议问题