Solved

Select string from within brackets - regex maybe ?

Posted on 2008-06-20
3
332 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 500 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now