【问题标题】:Surround a GUID with single quotes in C# using Regex使用正则表达式在 C# 中用单引号将 GUID 括起来
【发布时间】:2019-01-15 21:07:02
【问题描述】:

GUID 可以出现在带有或不带有单引号的语句中,例如

string inputString = @"ObjectID='{A591C480-2979-48ED-9796-5C3149472E7A}' and ObjectID={90f0fb85-0f80-4466-9b8c-2025949e2079}";

我的要求是:

如果 GUID 用单引号括起来,请跳过它。
如果没有用单引号括起来,就用单引号括起来。

我希望输出像

inputString = @"ObjectID='{A591C480-2979-48ED-9796-5C3149472E7A}' and ObjectID='{90f0fb85-0f80-4466-9b8c-2025949e2079}'"

inputString = @"ObjectID='{A591C480-2979-48ED-9796-5C3149472E7A}' and ObjectID={'90f0fb85-0f80-4466-9b8c-2025949e2079'}"

在单引号内查找 GUID 的正则表达式是

var quotedGuidMatches = Regex.Matches(inputString, @"'[({]?\s?[a-zA-Z0-9]{8}\s?[-]?\s?([a-zA-Z0-9]{4}\s?[-]?\s?){3}\s?[a-zA-Z0-9]{12}\s?[})]?'");

查找 GUID 的正则表达式是

var guidMatches = Regex.Matches(inputString, @"\b[({]?\s?[a-zA-Z0-9]{8}\s?[-]?\s?([a-zA-Z0-9]{4}\s?[-]?\s?){3}\s?[a-zA-Z0-9]{12}\s?[})]?\b");

用单引号包围 guid 的正则表达式是

inputString = Regex.Replace(inputString, @"\b[({]?\s?[a-zA-Z0-9]{8}\s?[-]?\s?([a-zA-Z0-9]{4}\s?[-]?\s?){3}\s?[a-zA-Z0-9]{12}\s?[})]?\b", "'$0'", RegexOptions.IgnoreCase);

不幸的是,这个发现所有 GUID 都在单引号内并且没有单引号。 replace 方法最终会围绕 guid 已经有单引号和多个单引号。
您能否帮我找到不在单引号内的 GUID。然后用单引号括起来。

【问题讨论】:

  • 目前还不是很清楚为什么要为此使用正则表达式。检查输入字符串的第一个和最后一个字符有什么问题?

标签: c# regex guid


【解决方案1】:

您可以匹配单引号内的 GUID 并捕获它,以便能够在匹配评估器内测试该组的匹配,并在所有其他将用单引号括起来的上下文中匹配 GUID:

var inputString = @"ObjectID='{A591C480-2979-48ED-9796-5C3149472E7A}' and ObjectID={90f0fb85-0f80-4466-9b8c-2025949e2079}";
var guid = @"[({]?\s?[a-zA-Z0-9]{8}\s?[-]?\s?(?:[a-zA-Z0-9]{4}\s?[-]?\s?){3}\s?[a-zA-Z0-9]{12}\s?[})]?";
inputString = Regex.Replace(inputString, $@"('{guid}')|{guid}", x =>
    x.Groups[1].Success ? x.Value : $"'{x.Value}'");
Console.WriteLine(inputString);
// => ObjectID='{A591C480-2979-48ED-9796-5C3149472E7A}' and ObjectID='{90f0fb85-0f80-4466-9b8c-2025949e2079}'

请参阅C# demo

请注意,我将原始模式中的([a-zA-Z0-9]{4}\s?[-]?\s?) 捕获组设为非捕获(?:[a-zA-Z0-9]{4}\s?[-]?\s?)

$@"('{guid}')|{guid}" 字符串字面量创建了一个类似

的正则表达式
('[({]?\s?[a-zA-Z0-9]{8}\s?[-]?\s?(?:[a-zA-Z0-9]{4}\s?[-]?\s?){3}\s?[a-zA-Z0-9]{12}\s?[})]?')|[({]?\s?[a-zA-Z0-9]{8}\s?[-]?\s?(?:[a-zA-Z0-9]{4}\s?[-]?\s?){3}\s?[a-zA-Z0-9]{12}\s?[})]?

第一个替代方案将单引号内的 GUID 匹配并捕获到组 1 中,第二个替代方案匹配其他上下文中的 GUID。 x => x.Groups[1].Success ? x.Value : $"'{x.Value}'" 行仅将匹配项与 's 括起来,前提是它尚未用单引号括起来。

【讨论】:

    【解决方案2】:

    如果不需要 Regex,为什么不使用简单的方法?

    void Main()
    {
        string ObjectID="'{A591C480-2979-48ED-9796-5C3149472E7A}'";
    
        if (!(ObjectID.StartsWith("'") && ObjectID.EndsWith("'")))
        {
            ObjectID = $"'{ObjectID.Trim('\'')}'";
        }
    
        Console.WriteLine(ObjectID);
    }
    

    我添加了对Trim 的调用,以防字符串在开头或结尾只有一个撇号。

    【讨论】:

    • 我读到了这个问题,因为 guid 嵌入了一个较长的字符串,如 "ObjectID='{A591C480-2979-48ED-9796-5C3149472E7A}' 和 ObjectID={90f0fb85-0f80-4466 -9b8c-2025949e2079}"
    • @AdamG - 是的,我明白了。这似乎是要求。我错过了。
    【解决方案3】:

    我想出了这个解决方案。这个想法是从有引号的 Guid 中删除引号。然后将引号添加到所有(裸)Guids。如果有任何其他解决方案可以找到精确的 Guid without 单引号并使用纯正则表达式用单引号括起来,我会接受它作为答案。

    using System;
    using System.Text.RegularExpressions;
    
    namespace RegexTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                string inputString = @"ObjectID = '{A591C480-2979-48ED-9796-5C3149472E7A}' and ObjectID = { 90f0fb85 - 0f80 - 4466 - 9b8c - 2025949e2079 }";
                Console.WriteLine("Before: ");
                Console.WriteLine(inputString);
    
                var quotedGuidMatches = Regex.Matches(inputString, @"'[({]?\s?[a-zA-Z0-9]{8}\s?[-]?\s?([a-zA-Z0-9]{4}\s?[-]?\s?){3}\s?[a-zA-Z0-9]{12}\s?[})]?'");
    
                var guidMatches = Regex.Matches(inputString, @"\b[({]?\s?[a-zA-Z0-9]{8}\s?[-]?\s?([a-zA-Z0-9]{4}\s?[-]?\s?){3}\s?[a-zA-Z0-9]{12}\s?[})]?\b");
    
                //First eliminate single quotes from guoted guids
                foreach(var quotedGuid in quotedGuidMatches)
                {
                    inputString = inputString.Replace(quotedGuid.ToString(), quotedGuid.ToString().Trim('\''));
                }            
    
                //After single quotes have been eliminated from guids, surround all naked guids with single quotes
                inputString = Regex.Replace(inputString, @"\b[({]?\s?[a-zA-Z0-9]{8}\s?[-]?\s?([a-zA-Z0-9]{4}\s?[-]?\s?){3}\s?[a-zA-Z0-9]{12}\s?[})]?\b", "'$0'", RegexOptions.IgnoreCase);
    
                Console.WriteLine("\nAfter: ");
                Console.WriteLine(inputString);
    
                Console.ReadLine();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-05-11
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      • 2014-09-10
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      • 2010-10-05
      相关资源
      最近更新 更多