Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Input String :
"message(hello) {message= msg.hello;}xyz('Text1','Text2', Text3);asdgfd main.send(msg);"

Output:

'Text1'
'Text2'
Text3

I need to match "xyz('Text1','Text2', Text3)" from the above string and need to get each word separated by a comma(,) within the bracket. Can anyone give the proper regex to do this?

What I have tried:

I have tried but cannot able to get that separated values properly
Posted
Updated 21-Feb-19 5:32am
Comments
Richard Deeming 22-Feb-19 11:28am    
It sounds like you're trying to parse some code to an Abstract Syntax Tree[^]. Regular expressions are not a good solution to do that.

To be honest with you, I'd do it with two regexes: the first to extract the comma separated values, which is pretty trivial:
(?<=xyz\().+?(?=\))

Then either Split the string on commas, or parse it with a second, simple regex:
([^,]+?)(?=,|$)
Wich gives you each element in a separate Match.

That way, laters changes are a whole load easier to work out - complex regexes are hard to read and even harder to modify!
 
Share this answer
 
Comments
Piraisudan 21-Feb-19 14:05pm    
Hi... Thanks for your answer...

Small Update, My text should be like this...
"message(hello) {message= msg.hello;}xyz('Text1','Text2', Text3());asdgfd main.send(msg);"

Your first regex matches until "'Text1','Text2', Text3(" only. Can u please provide the solution to match what I need.

Also I modified like this "(?<=cmdSubmit\().+?[^()](?=\))" but it gives exception like "regex_error(error_syntax)" in c++ std library
Non-regex solution, not elegant, but...

string s = "message(hello) {message= msg.hello;}xyz('Text1','Text2', Text3);asdgfd main.send(msg);";

var parts = s.Split(new string[]{"xyz(", ");"}, StringSplitOptions.RemoveEmptyEntries)
	.Where(x=>x.Contains(","))
	.SelectMany(x=>x.Split(new string[]{","}, StringSplitOptions.RemoveEmptyEntries))
	.ToList();
 
Share this answer
 
Quote:
I need to match "xyz('Text1','Text2', Text3)" from the above string

When doing RegEx, it is a good idea to show a few examples to highlight the parts that are fixed and the parts that are variables.

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx:
Regexper[^]
 
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