Help - Search - Members - Calendar
Full Version: Regex is driving me mad!
Pixel2Life Forum > Help Section > Desktop Programming
Glasseater
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;

CODE

[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;

CODE

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;
CODE
[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.
MetalSkin
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.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.