regex matching

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

regex matching

Dave-32
#define BOOST_REGEX_DYN_LINK

#include <iostream>
#include <boost\regex.hpp>

using namespace boost;
using namespace std;

int main()
{
 regex reg_exp("\xF8.\x2A..\x0D(P|V)[0-9A-F]{3}");

 // I would expect this to match.
 cout << regex_match("\xF8p\x2Aqf\x0DPFFF", reg_exp) << endl;

 // I would expect this to not match due to the 'w'.
 // Why does it match?
 cout << regex_match("\xF8p\x2Aqfw\x0DPFFF", reg_exp) << endl;
}



_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: regex matching

Eric Niebler
Dave wrote:

> #define BOOST_REGEX_DYN_LINK
>
> #include <iostream>
> #include <boost\regex.hpp>
>
> using namespace boost;
> using namespace std;
>
> int main()
> {
>  regex reg_exp("\xF8.\x2A..\x0D(P|V)[0-9A-F]{3}");
>
>  // I would expect this to match.
>  cout << regex_match("\xF8p\x2Aqf\x0DPFFF", reg_exp) << endl;
>
>  // I would expect this to not match due to the 'w'.
>  // Why does it match?
>  cout << regex_match("\xF8p\x2Aqfw\x0DPFFF", reg_exp) << endl;
> }


Because '\x2A' is '*' which is a regex meta-character which means "match
the previous thing zero or more times.


--
Eric Niebler
Boost Consulting
www.boost-consulting.com
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: regex matching

John Maddock
In reply to this post by Dave-32
> // I would expect this to not match due to the 'w'.
> // Why does it match?
> cout << regex_match("\xF8p\x2Aqfw\x0DPFFF", reg_exp) << endl;

I believe the \x2A character is  a '*' right?

If so the compiler converts \x2A to a * so the regex engine sees a ".*".

If you double up those escapes and use "\\x2A" then the regex engine will
parse the escape rather than the compiler, and so it won't be interpreted as
a quantifier anymore.

This one had me fooled for a while BTW ;-)

John.

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users