删除时逗号和括号领域中存在串逗号没有括号内考虑逗号 [英] Delete the comma from the string when comma and parenthesis exists in field without considering comma within the parenthesis

查看:174
本文介绍了删除时逗号和括号领域中存在串逗号没有括号内考虑逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用C#的CSV的正则表达式可以包含:
代码



  A一个,(A,b,(一))。 



我曾尝试以下内容:

  VAR的结果=(从Regex.Matches匹配M(输入@([^()] +(\([^()] * \))?)选择m.Value).ToArray(); 
串RESULT1 = ConvertStringArrayToString(结果);
textBox2.Text = RESULT1;

但我得到的输出是这样的:

  A 
A
A
B(一)


解决方案

您需要< A HREF =https://msdn.microsoft.com/en-us/library/bs2twtah(v=vs.110).aspx#balancing_group_definition相对=nofollow>均衡组定义,以使其工作:

  [^()] *(?:\(([^()] * |(小于开放> \()|(?< -open> \)))*((开)(?))\))* [^,()] * 

演示



一个很好的介绍到BGD是这里和的这里


I'm trying to use a regular expression in C# csv that can contain: code as

a,a,(a,b,(a)).

I have tried the following:

var result = (from Match m in Regex.Matches(input, @"([^,()]+(\([^()]*\))?") select m.Value).ToArray();
string result1 = ConvertStringArrayToString(result);
textBox2.Text = result1;

but i get output like this:

a
a
a
b(a)

解决方案

You need balancing group definitions to make it work:

[^,()]*(?:\(([^()]*|(?<open>\()|(?<-open>\)))*(?(open)(?!))\))*[^,()]*

DEMO

A good introduction into BGD is here and here

这篇关于删除时逗号和括号领域中存在串逗号没有括号内考虑逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆