1

I need string with brackes from one string using regex. My string will be like: Node{id}data_{name}key

So I need id and name with comma seperated Result: id, name.

Can I have regex expression for the same?

2 Answers 2

7

How about

Node{+(?<id>[0-9]*)+}data_{+(?<name>[a-zA-Z]*)}key

You tagged with C# so how about code something like the following (not tested)

Regex exp = new Regex(@"Node{+(?<id>[0-9]*)+}data_{+(?<name>[a-zA-Z]*)}key", RegexOptions.IgnoreCase);
MatchCollection matchList = exp.Matches(yourString);
string id = matchList.Groups["id"].Value;
string name = matchList.Groups["name"].Value;
//build up whatever string you like
return String.Format("{0},{1}", id, name);
Sign up to request clarification or add additional context in comments.

3 Comments

No worries. How about a tick? :)
out of curiosity and in case anyone's listening, will the same pattern work in Java?
It should, it's just regex
0

You cannot do this with only one regex. However you can extract the list of strings within braces by matching against \{([^}]+)\} in a loop, extract \1 and put it into an array.

Then join the elements of the array with , and you will have your result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.