Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Select string from within brackets - regex maybe ?

Posted on 2008-06-20
3
Medium Priority
?
346 Views
Last Modified: 2013-12-16
I have a string which looks something like this
{Rare,Medium,Gennemstegt}{Pommes,Bagt Kartoffel{Hvidløg,Krydder,Smør}}{Sovs1,Sovs2,Sovs3}

it can ofcause change alot - depending on user input

does anyone know a split command or alike which could give me the following result
arr(0) = {Rare,Medium,Gennemstegt}
arr(1) = {Pommes,Bagt Kartoffel{Hvidløg,Krydder,Smør}}
arr(2) = {Sovs1,Sovs2,Sovs3}

I was thinking of simply looping through the string and counting the number of { and  } once these were equal i would have my first string, then proceed however could this be done by a Regular Expression or ?
0
Comment
Question by:mSchmidt
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
NeoTeq earned 2000 total points
ID: 21833000
The attached code does the job. Match groups 1 through 3 return your desired values. It should theoretically work with more than three groups, but that's untested.
using System;
using System.Text.RegularExpressions;
 
namespace Test {
	class Program {
		static void Main(string[] args) {
			string input = "{Rare,Medium,Gennemstegt}{Pommes,Bagt Kartoffel{Hvidløg,Krydder,Smør}}{Sovs1,Sovs2,Sovs3}";
			
			Match match = Regex.Match(input, "^({.*?})((?<=}){.*?}(?={))({.*?})$");
			
			foreach (Group group in match.Groups) {
				Console.WriteLine(group.Value);
			}
			
			Console.ReadLine();
		}
	}
}

Open in new window

0
 

Author Comment

by:mSchmidt
ID: 21833560
Yes very good, however why does it also return the entire string ?
0
 
LVL 10

Expert Comment

by:NeoTeq
ID: 21833596
It does that because the entire regular expression matches the entire string... The three matching groups afterwards are the ones I told it to match with braces ( and ).
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question