Regular Expression (vers. 1.34.1)

Hi there, I'm trying to use boost regular expression for the first time and I'm having some trouble getting it right. Current I'm trying to find all files with the a certain extension (by also using boost::filesystem), which should be simple, but I'm probably missing something. Here is the expression I'm using. boost::regex regularExpression; std::string expression("\.\/\w+(\.txt)"); try { regularExpression.assign(expression, boost::regex_constants::icase); ... std::string testAgainst((*iterPath).string()); if (boost::regex_match(testAgainst, regularExpression)) ... which (I thought) should match all string with the pattern "./filename.txt" but it does not. Using "\.\/\ReadMe(\.txt)" for example, correctly matches the ./ReadMe.txt file, so the \w+ seems not do what I'm reading from [1] where (\w+\.) is used to match part of a URL. Anyone can shine some light on this, or point me to another good reading about regular expression with boost? Note: I'm using version 1.34.1 because I have to. Thanks, S. [1] http://onlamp.com/pub/a/onlamp/2006/04/06/boostregex.html

Salvatore Benedetto wrote:
Here is the expression I'm using.
boost::regex regularExpression; std::string expression("\.\/\w+(\.txt)");
I haven't reviewed the syntax of the regular expression or the code you're using to match it. But I can tell you that your backslashes are not being passed to the library as you expect because of the rules of ordinary C++ string literals. This might (or might not) work better: "\\.\\/\\w+(\\.txt)"

AMDG Salvatore Benedetto wrote:
On 5 May 2010 16:10, Nat Goodspeed
wrote: This might (or might not) work better: "\\.\\/\\w+(\\.txt)"
Indeed I was missing a backslash.
The following worked as I expected.
std::string expression("\.\/\\w+(\.txt)");
Actual not. The first level of backslashes is processed by C++. The regex library never sees them. This pattern with match x/filename-txt, since . is a wild card. In Christ, Steven Watanabe

On 5 May 2010 16:23, Steven Watanabe
AMDG
Salvatore Benedetto wrote:
On 5 May 2010 16:10, Nat Goodspeed
wrote: This might (or might not) work better: "\\.\\/\\w+(\\.txt)"
Indeed I was missing a backslash.
The following worked as I expected.
std::string expression("\.\/\\w+(\.txt)");
Actual not. The first level of backslashes is processed by C++. The regex library never sees them. This pattern with match x/filename-txt, since . is a wild card.
Oh so it's just a case that it matches string "./File.txt". So basically I have to double backslash everytime? Thanks again, S.

Actual not. The first level of backslashes is processed by C++. The regex library never sees them. This pattern with match x/filename-txt, since . is a wild card.
Oh so it's just a case that it matches string "./File.txt".
So basically I have to double backslash everytime?
Everytime you want the regex engine to see a "\" you have to put "\\" in your string since the first \ gets swallowed by the C++ parser and doesn't appear in the actual string placed in your program (of course if the regular expressions were read in from an extern text file or something then this wouldn't be the case). HTH, John.

Salvatore Benedetto wrote:
So basically I have to double backslash everytime?
If you want that backslash to make it to the regex engine, yes. Otherwise, you may use raw string literals if your compiler supports them. GCC 4.5 is supposed to support it, but it appears completely broken.

Le 05/05/2010 17:20, Mathias Gaunard a écrit :
Salvatore Benedetto wrote:
So basically I have to double backslash everytime?
If you want that backslash to make it to the regex engine, yes.
Otherwise, you may use raw string literals if your compiler supports them. GCC 4.5 is supposed to support it, but it appears completely broken.
My bad, I had a wrong build, raw string literals appear to work in 4.5
participants (5)
-
John Maddock
-
Mathias Gaunard
-
Nat Goodspeed
-
Salvatore Benedetto
-
Steven Watanabe