Using zero characters in regular expessions
Hello, i try to use the regex library for binary data. I tried this code: string h("\x81\x05\x00\x48"); regex e2("\x81\x05\x00(.)"); smatch m; if (regex_match(h, m, e2)) { cout << "0. matched=" << (m[0].matched?"true":"false") << endl; cout << "0. match length=" << m[0].str().size() << endl; cout << "0. match character=" << m[0].str() << endl; cout << "1. matched=" << (m[1].matched?"true":"false") << endl; cout << "1. match length=" << m[1].str().size() << endl; cout << "1. match character=" << m[1].str() << endl; } But regex seems to have problems with the zero character because the result is: 0. matched=true 0. match length=2 0. match character=ü♣ 1. matched=false 1. match length=0 1. match character= If i change the code above to: string h("\x81\x05\x01\x48"); regex e2("\x81\x05\x01(.)"); and now the result is like intended: 0. matched=true 0. match length=4 0. match character=ü♣☺H 1. matched=true 1. match length=1 1. match character=H Technical it should be possible to work with zero characters in basic_string. Is this a bug of regex or is this technical not possible? Keno Basedow GEDIS GmbH Sophienblatt 100 D-24114 Kiel Fon: +49-431-60051-24 (-0) Fax: +49-431-60051-11 basedow@gedis-online.de
i try to use the regex library for binary data. I tried this code:
string h("\x81\x05\x00\x48"); regex e2("\x81\x05\x00(.)"); smatch m; if (regex_match(h, m, e2)) { cout << "0. matched=" << (m[0].matched?"true":"false") << endl; cout << "0. match length=" << m[0].str().size() << endl; cout << "0. match character=" << m[0].str() << endl; cout << "1. matched=" << (m[1].matched?"true":"false") << endl; cout << "1. match length=" << m[1].str().size() << endl; cout << "1. match character=" << m[1].str() << endl; }
The thing is if you pass a const char* to either basic_string or boost::regex then it will assume that the string is null-terminated, which as you have found does the wrong thing in this case. In both cases use the constructor that takes a string length as well as a pointer: string h("\x81\x05\x00\x48", 4); regex e2("\x81\x05\x00(.)", 7, regex::perl); Hope this helps, John.
participants (2)
-
John Maddock
-
Keno.Basedow@gedis.rohde-schwarz.com