正则表达式替换括号内的逗号C#

时间:2016-01-05 18:08:28

标签: c# regex replace

我有一段时间只用括号内的逗号来替换此字符串:

  

选择distinct \ n(''+ rtrim(ent.custno)+' -   '+ rtrim(ent.company)+'')作为客户,ent.phone,0 as   curr,\ n \ n(从billmast中选择SUM(billmast.balance)作为余额)   billmast.billid = ent.entid和ent.cid ='abis'和   billmast.balance<> 0和billmast.invdate> dateadd(day,-60,getdate())   和billmast.invdate0和billmast.invdate> dateadd(day,-90,getdate())   和billmast.invdate0和billmast.invdate> dateadd(day,-120,getdate())   和billmast.invdate0和billmast.adddate

我尝试了什么:

        //// replaces nothing
        //cols = Regex.Replace(cols, ",*(?=[^(]*\\))", m => m.Value.Replace(",", ";"));

        //// adds semi-colon between each character inside parentheses
        //cols = Regex.Replace(cols, ",*(?=[^(]*\\))", ";");

        //// replaces nothing
        //cols = Regex.Replace(cols, ",(?=[^(]*\\))", ";");

        //// replaces nothing
        //cols = Regex.Replace(cols, ",(?=[^(]*\\))", m => m.Value.Replace(",", ";"));

        //// replaces nothing
        //cols = Regex.Replace(cols, @",(?=[^()]*\))", ";");

        //// replaces nothing
        //cols = Regex.Replace(cols, @",(?=[^()]*\))", m => m.Value.Replace(",", ";"));

        //// adds semi-colon between each character inside parentheses
        //cols = Regex.Replace(cols, ",*(?=[^()]*\\))", ";");

        // replaces all commas with semi-colon - not just ones in parentheses
        //cols = Regex.Replace(cols, ",(?![^(]*\\))", ";");

......还有很多其他的事情。

,(?![^(]*\\) 

似乎在下面的演示中工作,但不在C#中 https://regex101.com/r/mO1bZ5/1

2 个答案:

答案 0 :(得分:1)

假设您的括号已配对且可以嵌套(平衡),并且您需要替换这些平衡括号内的所有逗号,您可以使用正则表达式匹配这些子字符串并用匹配评估程序替换逗号:

-(UIColor*)getRandomColor{

    CGFloat red = arc4random_uniform(255) / 255.0f;
    CGFloat green = arc4random_uniform(255) / 255.0f;
    CGFloat blue = arc4random_uniform(255) / 255.0f;

    return [UIColor colorWithRed:red green: green blue:blue alpha:1.0f];
}

请参阅IDEONE demo

答案 1 :(得分:0)

问题是你的字符串包含嵌套的括号。您的模式必须考虑到这一事实并描述最终的嵌套括号,例如在此示例字符串中:(aaaaa,bbbbb(cccc(dddd)eeee)ffff)以确保右括号是好的。

string pattern = @",(?=(?>[^()]+|\((?<depth>)|\)(?<-depth>))*?(?(depth)(?!))\))";
cols = Regex.Replace(cols, pattern, ";");

命名的捕获(在此处没有捕获任何内容)就像一个计数器。

每次遇到左括号时,计数器都会递增,每次遇到右括号时,计数器都会递减。最后,条件(?(depth)...)检查计数器(在示例中名为depth),如果它不为null,则强制模式失败,总是失败的断言(?!)(字面意思是:后面没有任何内容)。

细节:

,
(?=
    (?>                 # open an atomic group: important because a simple 
                        # non-capturing group(?:[^()]+)* may cause a catastrophic
                        # backtracking if the end of the pattern doesn't succeed.

        [^()]+          # all that is not a parenthesis
      |
        \( (?<depth>)   # a opening parenthesis increments the counter
      |
        \) (?<-depth>)  # a closing parenthesis decrements the counter
    )*?                 # a non-greedy quantifier is used because the group must
                        # be repeated until the end of the pattern succeeds
                        # (it prevents the regex engine to match all the string
                        # and to give back characters until the pattern succeeds)

    (?(depth) (?!) )    # check the counter and forces to fail when not null
    \)
)