Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't boost regex '.{2}' match '??'

I'm trying to match some chunks if interesting data within a data stream.

There should be a leading < then four alphanumeric characters, two characters of checksum (or ?? if no shecksum was specified) and a trailing >.

If the last two characters are alphanumeric, the following code works as expected. If they're ?? though it fails.

// Set up a pre-populated data buffer as an example
std::string haystack = "Fli<data??>bble";

// Set up the regex
static const boost::regex e("<\\w{4}.{2}>");
std::string::const_iterator start, end;
start = haystack.begin();
end = haystack.end();
boost::match_flag_type flags = boost::match_default;

// Try and find something of interest in the buffer
boost::match_results<std::string::const_iterator> what;
bool succeeded = regex_search(start, end, what, e, flags); // <-- returns false

I've not spotted anything in the documentation which suggests this should be the case (all but NULL and newline should be match AIUI).

So what have I missed?

like image 312
Jon Cage Avatar asked Nov 08 '16 09:11

Jon Cage


People also ask

What is boost regex?

Boost. Regex allows you to use regular expressions in C++. As the library is part of the standard library since C++11, you don't depend on Boost. Regex if your development environment supports C++11. You can use identically named classes and functions in the namespace std if you include the header file regex .

How to match character in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

What does d mean in regex?

04/29/2019. From the . NET documentation of Regex , \d matches any decimal digit. The signification of a "decimal digit" depends on the options of the regex: Without RegexOptions.


1 Answers

Because ??> is a trigraph, it will be converted to }, Your code is equivalent to:

// Set up a pre-populated data buffer as an example
std::string haystack = "Fli<data}bble";

// Set up the regex
static const boost::regex e("<\\w{4}.{2}>");
std::string::const_iterator start, end;
start = haystack.begin();
end = haystack.end();
boost::match_flag_type flags = boost::match_default;

// Try and find something of interest in the buffer
boost::match_results<std::string::const_iterator> what;
bool succeeded = regex_search(start, end, what, e, flags); // <-- returns false

You can change to this:

std::string haystack = "Fli<data?" "?>bble";

Demo (note: I use std::regex which is more or less the same)

NOTE: trigraph is deprecated from C++11, will be (likely) removed from C++17

like image 175
Danh Avatar answered Oct 19 '22 06:10

Danh