Solved

Select string from within brackets - regex maybe ?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

22 Experts available now in Live!

Get 1:1 Help Now