regex help

Hello forum,
I am having an issue with the regular expressions in the C++11 extension. Below is a surrogate for my code...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
		regex rgx;
		smatch result;
		for (int i=0; i<num_var; i++ )
		{
			rgx.assign("%\(.*\)");
			do some stuff with this value of rgx...

			while (infile.peek() != '\n')
			{
				getline(infile,stmp1);
				rgx.assign("\..*:[1-999]");
			        do some stuff with the new value of rgx...	
			}
			...
		}


what happens is that the first rgx.assign() works, but the second gives me an error at runtime.

terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
Aborted

Does anyone know how I can reassign the regular expression in the rgx variable? Thanks
Last edited on
Also, I am new to regular expressions but I think those are right...would someone be able to check if those are correct for searching the patterns

%(NAME)

and

.NAME:#

where NAME can be any letter andthe # can take any number between 1-999? The first seems to work, but the compiler gives me warnings about the \) in the first and the \. in the second. Thanks
Last edited on
...but the compiler gives me warnings about the \) in the first and the \. ...

That's because you should escape the backslashes. i.e.

"%\\(.*\\)"

and

"\\..*:[1-999]"

The first regex is correct for finding %(NAME) where NAME can be a string of ANY character. Also watch out for the "greediness" of the * operator. It may match more than you want. For instance, your first regex will match this whole string "%(abc123) Hello (Hi)". If you only want to match the "%(abc123)" then you will have to modify your regex.

and the # can take any number between 1-999?

The error in the second regex is due to the multiple nines. [1-999] will not match any number between 1 and 999. It's an error because what [1-999] means is "match any character that's any digit between 1 and 9 or 9 or 9". There should be only unique characters in the character class.

If you want to match any number between 1 and 999 you would have to do something like
[1-9]\d{0,1}\d{0,1} where \d is shorthand for [0-9] and the {0,1} means at least zero times but no more than one time.
Last edited on
thanks for the reply. I don't understand why I have to escape the backslash though...I thought \( and \) were correct for looking for the actual opening and closing perenthesis. If I escape the backslashed... wouldn't it be searching for something that starts with %\ as opposed to %( and for closing it would be looking for a \ rather than a )?
It's because your string has to get past the C++ compiler first. The compiler will look for backslashes in double quotes and try to use it as an escape sequence. For instance, \n (newline) and \t (tab) are escape sequences. It will complain about unknown escape sequences if invalid characters come after the slash.

The string "%\\(.*\\)" will actuallly be sent as %\(.*\) to the regex parser.
ahhhh I see. Thanks!
So I think the error that I am getting is because it doesn't like the regular expressions I am using. If I put in a simple one, it runs without giving the error. Is there anything you can see wrong with this (again trying to match something in the form of .NAME:# ) ?

rgx.assign("\\..*:[1-9]");

(I am just trying to match any number between 1-9 for now...I'll switch it to 1-999 later). If I just do


rgx.assign("\\..*:");

it works so it seems to not like the [1-9] part???

Last edited on
What's your compiler/library? "what(): regex_error" sounds suspiciously like GNU libstdc++, which did NOT implement the tr1/std regex library. Even though it has the header and some of the functions compile, they do not work. GCC is really falling behind on C++11 this year, still without regex or locale-independent unicode.

To use your example, this test:
1
2
3
4
5
6
7
8
9
10
11
#include <regex>
#include <iostream>
int main()
{
    std::regex re("\\..*:[1-9]");
    std::string s = ".NAME:3";
    if(std::regex_match(s, re))
        std::cout << "match\n";
    else
        std::cout << "no match\n";
}

prints "match" for me when compiled with clang++ 3.2 against LLVM libc++ and Visual Studio 2012 in default configuration, but throws an exception with GNU libstdc++.

With boost.regex, it prints "match" when compiled with GCC of course.
ok thank you. I did some searching around and it looks you are correct that GCC does not have it. I tried the intel 64bit 2013 compilers and I am getting the same issue. Oh well, I guess I'll have to try and do this another way or try and learn python...
The "other way" is to use boost library, it is cross platform and one of the most widely used C++ library:
http://www.boost.org/
Topic archived. No new replies allowed.