简体   繁体   English

正则表达式从字符串中提取用逗号分隔的字符串

[英]Regex to extract string separated by comma from a string

I would like to extract all the values separated by comma( , ) after matching with pattern. 我想在与pattern匹配后提取所有用逗号( , )分隔的值。

So first I am matching my string with regex and then using Matcher to extract values. 因此,首先我用正则表达式匹配我的字符串,然后使用Matcher提取值。

Regex= \\(([^)]+)\\) is matching correctly following string without any issue . Regex = \\(([^)]+)\\)正确匹配字符串后没有任何问题。

('A', '36254632546', 0, 'Test, test1', NULL) 

But unable to match when string convert(datetime, 'Dec 27 2016 10:36:54', 116) is available in original string. 但是当原始字符串中包含字符串convert(datetime, 'Dec 27 2016 10:36:54', 116)时无法匹配。 I tried matching last ) by putting $ at the end but seems not working. 我尝试将$放在末尾来匹配last ) ,但似乎无法正常工作。

String to match = ('A', convert(datetime, 'Dec 27 2016 10:36:54', 116), 0, 'Test, test1', NULL) 要匹配的字符串= ('A', convert(datetime, 'Dec 27 2016 10:36:54', 116), 0, 'Test, test1', NULL)

This is because your regex stops at the first ')'. 这是因为您的正则表达式在第一个')'处停止。

What you say in regex is start with '(' then group everything except ')' followed by ')' so then it stops even if you put dollar sign at the end because the end is not there you have remaining string after the first ')'. 您在正则表达式中说的是以'('开始,然后将除')'以外的所有内容都分组,然后以')'结束,所以即使您将美元符号放在末尾,它也会停止,因为末尾不存在,第一个'之后还有剩余的字符串)'。

The '+' sign is for consecutive chars. “ +”号用于连续的字符。

If you would like to match all the string you have to tell something like 如果您想匹配所有字符串,则必须告诉类似

\\\\(([^)]+\\\\)[^)]+)\\\\);

but this has to do with your program's logic. 但这与程序的逻辑有关。

Also if you want to much groups of commas you have to change your regex, so that the groups are groups of commas. 另外,如果要使用许多逗号,则必须更改正则表达式,以便这些组是逗号的组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM