0

I have such a regular expression

boost::regex isAgent
    ("Mozilla/\d[.]\d \(Windows NT \d[.]\d; (Win64; x64;|WOW64;)?(.*?)\) Gecko/\d{8} Firefox/\d\d[.]\d",
    boost::regex::perl);
if (boost::regex_search(auxAgent.c_str(), match, reg)){...}...

i know that in auxAgent i have exacly Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0

on this page http://gskinner.com/RegExr/?37em3 everything matches but not in boost, what am i doing wrong?

whd
  • 1,819
  • 1
  • 21
  • 52

2 Answers2

1

In C++, the character \ needs to be escaped. So if you want to escape anything, you need to do \\. That should fix the problem. Whenever you use the backslash in a string, you need to escape it like that. If you ever need to find it in a string with the regex, you'll need to search for it with \\\\.

Paweł Stawarz
  • 3,952
  • 2
  • 17
  • 26
  • Pawel is correct. To clarify, this means that your `isAgent` variable should be defined like so: `boost::regex isAgent ("Mozilla/\\d\\.\\d \\(Windows NT \\d\\.\\d; (Win64; x64;|WOW64;)?(.*?)\\) Gecko/\\d{8} Firefox/\\d\\d\\.\\d", boost::regex::perl);` – Tim Pierce Dec 01 '13 at 05:22
1

I think Pawel Stawarz is correct. You should escape the backslashes. But here are all the characters you need to escape:

^ . $ | ( ) [ ] * + ? \ /

Replace \ with \\

and

Replace ? with \?

etc.

Source: How to escape a string for use in Boost Regex

Community
  • 1
  • 1
Naveed Hasan
  • 641
  • 7
  • 19