Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get substring betweet two { } using regex.
I have data like this
CSS
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
Comments
Andy411 21-Nov-12 3:33am    
Is this one string with CR/LF or do you have seperate strings for each value?
jaideepsinh 21-Nov-12 4:32am    
this string is continuous not separate string.

I improved my solution according to your comment. I suppose, that there are no CR/LF in your string.

C#
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[^]
 
Share this answer
 
v4
Comments
jaideepsinh 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 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.
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);
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900