简体   繁体   中英

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.

So first I am matching my string with regex and then using Matcher to extract values.

Regex= \\(([^)]+)\\) is matching correctly following string without any issue .

('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. I tried matching last ) by putting $ at the end but seems not working.

String to match = ('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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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