Jump to content


Photo

Regex is driving me mad!


  • Please log in to reply
1 reply to this topic

#1 Glasseater

Glasseater

    Young Padawan

  • Members
  • Pip
  • 7 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 06 May 2007 - 09:38 AM

Hey guys, I'm just looking for some help with a regular expression problem. I'm trying to make a database program that will read in the title (eg. [weapon=12]) of the section like this;

[weapon=1]damage=12;resistance=13;etc...

Right now, I'm just testing to see if I can read just the title by itself.
Here's my code that doesn't work;

string input = "[weapon=12]";	regex finder("^\[([a-zA-Z]+)=([0-9]+)\]$");	cmatch matches;	if(regex_match(input.c_str(), matches, finder))	{		cout << input << " matches " << finder << "." << endl;		for(int c = 0; c < matches.size(); c++)		{			cout << "\t#" << c << " - " << matches[c] << endl;		}	}	else	{		cout << "No matches were found." << endl;	}

This program gives me the following error:

This application has requested the Runtime to terminate in an unusual way. Please contact the application's support team for more information.

If I change the "finder" value to ^\[([a-zA-Z]+=([0-9]+)\]$ (I removed 1 bracket just before the =) it works, but returns this;
[weapon=12] matches ^\[([a-zA-Z]+=([0-9]+)\]$	#0 - [weapon=12]	#1 - 12
I was hoping it would return 'weapon' as value 1 and '12' as value 2.

I'm using Dev C++ 4.9.9.2 with Boost.Regex
Any help/advice would be much appreciated.

P.S. I also tested the first one in several regex testers, and all of them returned the proper values, so I really have no clue what wrong with this. In the documentation for Boost.Regex, they said that it uses Perl's same grammar and that's what I used to build this one.

Edited by Glasseater, 06 May 2007 - 09:43 AM.


#2 MetalSkin

MetalSkin

    Young Padawan

  • Members
  • Pip
  • 196 posts
  • Gender:Male
  • Location:Brisbane, Australia

Posted 25 June 2007 - 05:04 PM

Well your right about the regex syntax being correct, however I'm not 100% familiar with any perl peculiarities.

Found an interesting tutorial on the library that your using (boost.regex) which may or may not assist:

boost.regex tutorial

I presume your having a gander at the doco here.

Maybe someone who is more familiar with perl regex can assist.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users