当字段中存在逗号和括号时如何解析逗号分隔的字符串
我在 C# 中有这个字符串,
adj_con(CL2,1,3,0),adj_cont(CL1,1,3,0),NG, NG/CL, 5 value of CL(JK), HO
我想使用正则表达式来解析它以获得以下内容:
adj_con(CL2,1,3,0)
adj_cont(CL1,1,3,0)
NG
NG/CL
5 value of CL(JK)
HO
除了上面的示例之外,我还使用以下内容进行了测试,但仍然无法正确解析它。
"%exc.uns: 8 hours let @ = ABC, DEF", "exc_it = 1 day" , " summ=graffe ", " a,b,(c,d)"
新文本将位于一个字符串中
string mystr = @"""%exc.uns: 8 hours let @ = ABC, DEF"", ""exc_it = 1 day"" , "" summ=graffe "", "" a,b,(c,d)""";
I have this string in C#
adj_con(CL2,1,3,0),adj_cont(CL1,1,3,0),NG, NG/CL, 5 value of CL(JK), HO
I want to use a RegEx to parse it to get the following:
adj_con(CL2,1,3,0)
adj_cont(CL1,1,3,0)
NG
NG/CL
5 value of CL(JK)
HO
In addition to the above example, I tested with the following, but am still unable to parse it correctly.
"%exc.uns: 8 hours let @ = ABC, DEF", "exc_it = 1 day" , " summ=graffe ", " a,b,(c,d)"
The new text will be in one string
string mystr = @"""%exc.uns: 8 hours let @ = ABC, DEF"", ""exc_it = 1 day"" , "" summ=graffe "", "" a,b,(c,d)""";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
事件速度更快:
这应该可以解决问题。基本上,寻找“函数指纹”或任何不带逗号的内容。
插入符号象征分组停止的位置。
Event faster:
That should do the trick. Basically, look for either a "function thumbprint" or anything without a comma.
The Carets symbolize where the grouping stops.
只是这个正则表达式:
测试示例:
返回
Just this regex:
A test example:
returns
如果您只是必须使用正则表达式,那么您可以将字符串拆分为以下内容:
它将您的输入分解为以下内容:
您还可以使用 .NET 的平衡分组结构来创建与嵌套括号一起使用的版本,但您可能只是以及非正则表达式解决方案之一。
If you simply must use Regex, then you can split the string on the following:
It breaks your input into the following:
You can also use .NET's balanced grouping constructs to create a version that works with nested parens, but you're probably just as well off with one of the non-Regex solutions.
实现 Snowbear 所做的事情的另一种方法是:
将任何非嵌套分隔符替换为另一个分隔符,然后您可以将其与普通
string.Split()
一起使用。您还可以选择使用哪种类型的括号 -()
、<>
、[]
,甚至是像这样奇怪的东西>\/
、][
或`'
。出于您的目的,您将使用该函数首先将您的字符串转换
为然后在
~
上分割,忽略嵌套的逗号。Another way to implement what Snowbear was doing:
The idea is to replace any non-nested delimiter with another delimiter that you can then use with an ordinary
string.Split()
. You can also choose what type of bracket to use -()
,<>
,[]
, or even something weird like\/
,][
, or`'
. For your purposes you would useThe function would first turn your string into
then split on the
~
, ignoring the nested commas.假设非嵌套、匹配括号,您可以轻松匹配所需的标记,而不用拆分字符串:
Assuming non nested, matching parentheses, you can easily match the tokens you want instead of splitting the string:
该模式匹配 ) 并将其从匹配中排除然后匹配 ,
或者
匹配 ,后跟一个空格。
结果 =
adj_con(CL2,1,3,0)
adj_cont(CL1,1,3,0)
异常
NG/CL
CL(JK)的5值
过氧化氢
The pattern matches for ) and excludes it from the match then matches ,
or
matches , followed by a space.
result =
adj_con(CL2,1,3,0)
adj_cont(CL1,1,3,0)
NG
NG/CL
5 value of CL(JK)
HO
TextFieldParser (msdn) 类似乎具有内置功能:
请参阅文章,它帮助我找到了
The TextFieldParser (msdn) class seems to have the functionality built-in:
See the article which helped me find that
这是一个更强大的选项,它会解析整个文本,包括嵌套括号:
您可以通过迭代
match.Groups["Token"].Captures
来获取所有匹配项。Here's a stronger option, which parses the whole text, including nested parentheses:
You can get all matches by iterating over
match.Groups["Token"].Captures
.