Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Input String :
"int, float(2,3), varchar(20)"

Output:

int
float(2,3)
varchar(20)

I need each word separated by a comma(,) ignoring the commas inside brackets. Can anyone give the proper regex to do this especially in c++?

!without deleting the brackets

What I have tried:

std::string text = "int, float(2,3), varchar(20)";
std::regex re(R"(\}? +, +(?![^{]*\})\{?)");
std::sregex_token_iterator it(text.begin(), text.end(), re, -1);
std::sregex_token_iterator end;


output:

int
float(2,3
varchar(20
Posted
Updated 10-Jul-22 5:56am

1 solution

The regex you show won't do anything like that.

Try this:
(.+?\(.*?\))|(.+?(,|$))
It may not be exactly what you wanted, but it's a better start than you have.

If you are going to play with regexes, then get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.
But ... I'd strongly suggest that you use a tokenizer instead of a regex - software languages get very complex very quickly, and modifying complex regexes can get to be a very frustrating way to spend a lot of time! Regular expressions are a good tool, but so is a hammer, until you realize it's a screw you are trying to secure rather than a nail.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900