Unexpected results in matching a regex

D

Dave

#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, and it does.
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;
}
 
A

Andre Kostur

#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, and it does.
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;
}

You're asking in the wrong place.... that's not Standard C++. You'll be
much better off asking in the boost user's mailing lists and such.
 
V

Victor Bazarov

Dave said:
#define BOOST_REGEX_DYN_LINK

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

Should change it to

#include <boost/regex.hpp>

and _never_ again use backslashes in the header name.
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, and it does.
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;
}

Oh, did you try Boost online forum? What did they say?

V
 
D

Dave

Victor Bazarov said:
Dave said:
#define BOOST_REGEX_DYN_LINK

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

Should change it to

#include <boost/regex.hpp>

and _never_ again use backslashes in the header name.
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, and it does.
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;
}

Oh, did you try Boost online forum? What did they say?

V

Actually, yes I did post to Boost, but my message has not yet appeared. Your
comment on backslashes contributes absolutely nothing to this thread.
 
V

Victor Bazarov

Dave said:
Dave said:
#define BOOST_REGEX_DYN_LINK

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

Should change it to

#include <boost/regex.hpp>

and _never_ again use backslashes in the header name.

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, and it does.
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;
}

Oh, did you try Boost online forum? What did they say?

V


Actually, yes I did post to Boost, but my message has not yet appeared.

So, you're growing impatient, is that it? Generally, Boost functionality
is OT here. If you asked about std::tr1, you'd be closer to the topic of
this newsgroup.

Have you tried other tools, like Perl or 'sed' to see if the expression is
a match? No, I didn't. I just think that "\x2Aqfw" is not a match for
"\x2A..". You probably wanted to add another dot there.
> Your
comment on backslashes contributes absolutely nothing to this thread.

Not true, it promotes healthy coding practices and that is always
a GOOD THING(tm).

V
 
P

Pete Becker

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

// I would expect this to match, and it does.
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;
}

I can't speak for the Boost implementation, but our TR1 implementation
gets the same result. It's a little clearer with a slight rewriting of
the regular expression. Yours was:

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

In the ASCII encoding that's equivalent to:

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


And that embedded ".*.." will eat up two or more characters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top