4 Replies - 2439 Views - Last Post: 06 October 2009 - 07:15 PM Rate Topic: -----

#1 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 148
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Regex help, get all text until last :}

Posted 03 October 2009 - 06:55 AM

Hey everyone! I need help with my regex.
In this code
for(var i = 0 -> 100)
:{
say(i)
for(var i2 = 0 -> 20)
:{
:}
:}


Im trying to get all the text including the last :}
But my C# code always returns
for(var i = 0 -> 100)
:{
say(i)
for(var i2 = 0 -> 20)
:{
:}


My C# code is
Match mm = Regex.Match(loopcode, ":{.+?:}", RegexOptions.Singleline);
string code = mm.Value;


Can anyone help?

This post has been edited by Amrykid: 04 October 2009 - 11:53 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Regex help, get all text until last :}

#2 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 148
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: Regex help, get all text until last :}

Posted 04 October 2009 - 11:53 AM

BUMP! Can anyone help me?
Was This Post Helpful? 0
  • +
  • -

#3 masteryee  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: Regex help, get all text until last :}

Posted 04 October 2009 - 01:21 PM

Your regex matches the first :{ with the first :}. You need a slight modification to match the outer braces. Here's my code that iterates through groups recursively in case you need to match a ton of braces within one loop:

	Regex r = new Regex(@"\:\{(?:(.*\:\{.*\:\}.*)*|(?:(?!\:\{|\:\}).*(?=\:\}|\:\{))*)\:\}", RegexOptions.Singleline);
	Match mm = r.Match(textBox1.Text);
	while (mm.Success)
	{
		if (mm.Groups.Count > 0)
		{
			textBox2.Text += mm.Groups[0] + "\r\n";
			if (mm.Groups.Count > 1)
				mm = r.Match(mm.Groups[1].Value);
		}
	}



In short, this regex:

1) looks for :{
2) allows an inner :{ and :} to appear and text within and outside of these braces
3) OR allows only text to appear as long as it does not contain a :{ and :} pair
4) looks for a final :}

#3 may be a little hardcore, but it does the job. The goal of the regex is to allow inner braces to appear, or to allow text between the top braces. The goal of my C# code sample is to recursively check each inner pair of braces. Please keep in mind that you will still need to write C# code that will check for unmatched braces, i.e. your last matched group may contain 3 curly braces.

What you need to keep in mind though is that if you have a line with braces enclosed in quotes (a string variable) like var a = "Sad :{";, then my regex fails...
Was This Post Helpful? 1
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,466
  • Joined: 29-May 08

Re: Regex help, get all text until last :}

Posted 04 October 2009 - 01:42 PM

The regex will also matches braces inside of comments. :crazy:
It not easy to do. When I attempted to do Brace Matching I found it to be one the hardest piece of coding I did.
But its the most satisfying when you final crack it.
Was This Post Helpful? 1
  • +
  • -

#5 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 148
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: Regex help, get all text until last :}

Posted 06 October 2009 - 07:15 PM

View PostAdamSpeight2008, on 4 Oct, 2009 - 01:42 PM, said:

The regex will also matches braces inside of comments. :crazy:
It not easy to do. When I attempted to do Brace Matching I found it to be one the hardest piece of coding I did.
But its the most satisfying when you final crack it.

Thanks from both of you! I got it working! :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1