Reputation: 35
I have a problem in regular expression. I have a string and the string is validate with a regular expression and it is working with the python script but not in working in c++.
Working python code:
import re
txt = "\x01msvc-server\x1Cmsvc-xyzy4\x02<?xml version=\"1.0\" encoding=\"UTF-8\"?><SVCMessage currency=\"INR\" hostName=\"msvc-xyz4\" language=\"US-en\" retransmit=\"N\" sequence=\"00\" timeout=\"90\" version=\"8\"><Amount>0.01</Amount><BusinessDate>20190506</BusinessDate><CheckNumber>0</CheckNumber><LocalDate>20170506</LocalDate><LocalTime>160722</LocalTime><RequestCode>POINT_REDEMPTION</RequestCode><RevenueCenter>0</RevenueCenter><TerminalID>21</TerminalID><TraceID>190506860722N000000</TraceID><Track2>1161111112</Track2><TransactionEmployee>0</TransactionEmployee></SVCMessage>\x03\x04"
matcher = re.compile(r".*\x01([A-Za-z0-9_-]*)\x1C([A-Za-z0-9_-]*)\x02([^\x00-\x1F\x7F]*)\x03\x04.*")
results = matcher.match(txt)
if results == None:
print ('Invalid query , closed')
else:
print ('sucess')
My c++ code:
#include <iostream>
#include <regex>
using namespace std;
int main()
{
string a = "\x01msvc-server\x1Cmsvc-xyzy4\x02<?xml version=\"1.0\" encoding=\"UTF-8\"?><SVCMessage currency=\"INR\" hostName=\"msvc-xyz4\" language=\"US-en\" retransmit=\"N\" sequence=\"00\" timeout=\"90\" version=\"8\"><Amount>0.01</Amount><BusinessDate>20190506</BusinessDate><CheckNumber>0</CheckNumber><LocalDate>20170506</LocalDate><LocalTime>160722</LocalTime><RequestCode>POINT_REDEMPTION</RequestCode><RevenueCenter>0</RevenueCenter><TerminalID>21</TerminalID><TraceID>190506860722N000000</TraceID><Track2>1161111112</Track2><TransactionEmployee>0</TransactionEmployee></SVCMessage>\x03\x04";
// Here b is object of regex- Regular Expression
regex b(".*\x01([A-Za-z0-9_-]*)\x1C([A-Za-z0-9_-]*)\x02([^\x00-\x1F\x7F]*)\x03\x04.*");
cout<< a << endl;
if( regex_match(a, b)){
cout << "String is matches Reguler Expreation " << endl;
}else{
cout << "String are not match" << endl;
}
return 0;
}
And the expected the result is - String is match ... in c++
Upvotes: 1
Views: 132
Reputation: 2849
Most probably problem is in \x00
char which terminates regex string.
You initialize b
using string literal which invokes explicit basic_regex( const CharT* s, flag_type f = std::regex_constants::ECMAScript );
overload.
To avoid this issue you can try to initialize it using std::string
, which in turn can be initialized something like that:
char re[] = ".*\x01([A-Za-z0-9_-]*)\x1C([A-Za-z0-9_-]*)\x02([^\x00-\x1F\x7F]*)\x03\x04.*";
std::string re_str(re, sizeof(re));
Upvotes: 0
Reputation: 66210
What about doubling the \
in the regular expression string?
//.........VV...................VV...................VV......VV.........VV......VV...VV
regex b(".*\\x01([A-Za-z0-9-_]*)\\x1C([A-Za-z0-9-_]*)\\x02([^\\x00-\\x1F\\x7F]*)\\x03\\x04.*");
Otherwise you can use a raw literal string
// .....VVV...........................................................................VV
regex b(R"(.*\x01([A-Za-z0-9-_]*)\x1C([A-Za-z0-9-_]*)\x02([^\x00-\x1F\x7F]*)\x03\x04.*)");
Off Topic Unrequested Suggestion: avoid using namespace std;
and explicit std
using std::cout
, std::string
, std::regex
, etc.
Upvotes: 1