Solved

Select string from within brackets - regex maybe ?

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 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