Solved

Select string from within brackets - regex maybe ?

Posted on 2008-06-20
3
343 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

615 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