C# Regex for Guid

后端 未结 6 1223
情歌与酒
情歌与酒 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条回答
  • 2020-11-27 03:23

    This one is quite simple and does not require a delegate as you say.

    resultString = Regex.Replace(subjectString, 
         @"(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$", 
         "'$0'");
    

    This matches the following styles, which are all equivalent and acceptable formats for a GUID.

    ca761232ed4211cebacd00aa0057b223
    CA761232-ED42-11CE-BACD-00AA0057B223
    {CA761232-ED42-11CE-BACD-00AA0057B223}
    (CA761232-ED42-11CE-BACD-00AA0057B223)
    

    Update 1

    @NonStatic makes the point in the comments that the above regex will match false positives which have a wrong closing delimiter.

    This can be avoided by regex conditionals which are broadly supported.

    Conditionals are supported by the JGsoft engine, Perl, PCRE, Python, and the .NET framework. Ruby supports them starting with version 2.0. Languages such as Delphi, PHP, and R that have regex features based on PCRE also support conditionals. (source http://www.regular-expressions.info/conditional.html)

    The regex that follows Will match

    {123}
    (123)
    123
    

    And will not match

    {123)
    (123}
    {123
    (123
    123}
    123)
    

    Regex:

    ^({)?(\()?\d+(?(1)})(?(2)\))$
    

    The solutions is simplified to match only numbers to show in a more clear way what is required if needed.

    0 讨论(0)
  • 2020-11-27 03:26

    Most basic regex is following:

    (^([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})$) 
    

    or you could paste it here.

    Hope this saves you some time.

    0 讨论(0)
  • 2020-11-27 03:31

    I use an easier regex pattern

    ^[0-9A-Fa-f\-]{36}$
    
    0 讨论(0)
  • 2020-11-27 03:37

    In .NET Framework 4 there is enhancement System.Guid structure, These includes new TryParse and TryParseExact methods to Parse GUID. Here is example for this.

        //Generate New GUID
        Guid objGuid = Guid.NewGuid();
        //Take invalid guid format
        string strGUID = "aaa-a-a-a-a";
    
        Guid newGuid;
    
        if (Guid.TryParse(objGuid.ToString(), out newGuid) == true)
        {
            Response.Write(string.Format("<br/>{0} is Valid GUID.", objGuid.ToString()));
        }
        else
        {
            Response.Write(string.Format("<br/>{0} is InValid GUID.", objGuid.ToString()));
        }
    
    
        Guid newTmpGuid;
    
        if (Guid.TryParse(strGUID, out newTmpGuid) == true)
        {
            Response.Write(string.Format("<br/>{0} is Valid GUID.", strGUID));
        }
        else
        {
            Response.Write(string.Format("<br/>{0} is InValid GUID.", strGUID));
        }
    

    In this example we create new guid object and also take one string variable which has invalid guid. After that we use TryParse method to validate that both variable has valid guid format or not. By running example you can see that string variable has not valid guid format and it gives message of "InValid guid". If string variable has valid guid than this will return true in TryParse method.

    0 讨论(0)
  • 2020-11-27 03:37

    You can easily auto-generate the C# code using: http://regexhero.net/tester/.

    Its free.

    Here is how I did it:

    enter image description here

    The website then auto-generates the .NET code:

    string strRegex = @"\b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}\b";
    Regex myRegex = new Regex(strRegex, RegexOptions.None);
    string strTargetString = @"     {CD73FAD2-E226-4715-B6FA-14EDF0764162}.Debug|x64.ActiveCfg =         Debug|x64";
    string strReplace = @"""$0""";
    
    return myRegex.Replace(strTargetString, strReplace);
    
    0 讨论(0)
  • 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)" 
    
    0 讨论(0)
提交回复
热议问题