Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
I want to get substring betweet two { } using regex.
I have data like this
Title: {Title}
 Incident Description: {IncidentDescription}
Incident Level: {IncidentLevelName}
Incident Date: {IncidentDateAndTime}
Location: {Location}
 
I want data which are in between { } like i want Title,IncidentDescription,.....
using Regex.
Can any one help me how i solve this.
Posted 20-Nov-12 21:12pm
Comments
Andy411 at 21-Nov-12 3:33am
   
Is this one string with CR/LF or do you have seperate strings for each value?
jaydeepvjadeja at 21-Nov-12 4:32am
   
this string is continuous not separate string.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I improved my solution according to your comment. I suppose, that there are no CR/LF in your string.
 
string searchString = "Title: {MyTitle} Incident Description: {MyDescription} Incident Level: {MyLevel}";
string theValue = string.Empty;
string theDescription = string.Empty;
string theLevel = string.Empty;
string pattern = @"\{(?<myvalue>\w+)\}.+:\s*\{(?<mydescription>\w+)\}.+:\s*\{(?<mylevel>\w+)\}"; // continue the pattern for your needs
Regex rx = new Regex(pattern);
 
Match m = rx.Match(searchString);
 
if (m.Success)
{
    theValue = m.Groups["myvalue"].Value;
    theDescription = m.Groups["mydescription"].Value;
    theLevel = m.Groups["mylevel"].Value;
}
 
You can find more details here Regular Expression Language - Quick Reference[^]
  Permalink  
v4
Comments
jaydeepvjadeja at 21-Nov-12 8:26am
   
Thanks for answer.
But i just give example for string this string is not static it's dynamic generated by user that's why i don't know how many variable use for storing data.i just want data which are in between {} this.
Andy411 at 21-Nov-12 9:11am
   
If the string has a dynamic length and a variable count of parameters, you should probably consider a different approach. Are the curly brackets {} your tokens? The you could search for all brackets or split the string using String.Split.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

string searchString = "Title: {MyTitle} Incident Description: {MyDescription} Incident Level: {MyLevel}";
Regex r1 = new Regex(@"{\w*\}");
string[] data= new string[100];
MatchCollection match = r1.Matches(searchString );
for (int i = 0; i < match.Count; i++)
{
data[i] = Regex.Replace(match[i].Value, @"[{}]", string.Empty);
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 22 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100