Boost logo

Boost Users :

Subject: [Boost-users] boost::regex: Unexpected behaviour
From: Pascal Kesseli (pascal_kesseli_at_[hidden])
Date: 2009-07-31 06:39:35


Hi everyone

Please have a look at the following code:

1: const regex REPORT_HEADER("^[^()]+((?:\\([^()]*\\)){4,})$");
2: const regex REPORT_DATA_ITEM("\\(([^()]*?)\\)(.*)");
3: string headerLine =
"P.01(1090618220000)(0008)(15)(3)(1.5.0)(kW)(5.5.0)(kvar)(8.5.0)(kvar)";
4: smatch match;
5: if(regex_match(headerLine, match, REPORT_HEADER)) {
6: regex_match(lexical_cast<string>(match[1]), match,
REPORT_DATA_ITEM);
7: clog << match[1] << endl;
8: regex_match(lexical_cast<string>(match[2]), match,
REPORT_DATA_ITEM);
9: clog << match[1] << endl;
10: regex_match(lexical_cast<string>(match[2]), match,
REPORT_DATA_ITEM);
11: clog << match[1] << endl;
12: regex_match(lexical_cast<string>(match[2]), match,
REPORT_DATA_ITEM);
13: unsigned int numberOfEntries = lexical_cast<unsigned
int>(match[1]);
14: string tmp;
15: for(unsigned int i = 0; i < numberOfEntries; ++i) {
16: tmp = match[2];
17: clog << "1: " << tmp << endl;
18: if(regex_match(tmp, match, REPORT_DATA_ITEM)) {
19: clog << "2: " << match[1] << endl;
20: tmp = match[2];
21: clog << "3: " << tmp << endl;
22: if(regex_match(tmp, match, REPORT_DATA_ITEM)) {
23: clog << "4: " << match[1] << endl;
24: }
25: }
26: }
27: }

Important is actually only the trailing for loop. Running the code as it
reads above, the output is as expected:

1090618220000
0008
15
1: (1.5.0)(kW)(5.5.0)(kvar)(8.5.0)(kvar)
2: 1.5.0
3: (kW)(5.5.0)(kvar)(8.5.0)(kvar)
4: kW
1: (5.5.0)(kvar)(8.5.0)(kvar)
2: 5.5.0
3: (kvar)(8.5.0)(kvar)
4: kvar
1: (8.5.0)(kvar)
2: 8.5.0
3: (kvar)
4: kvar

However, changing line 18 to:
if(regex_match(lexical_cast<string>(match[2]), match, REPORT_DATA_ITEM)) {

and thus eliminating the temporary variable tmp, results in the following
output:

kW
kvar
1090618220000
0008
15
1: (1.5.0)(kW)(5.5.0)(kvar)(8.5.0)(kvar)
1:
1:

What might be the problem here? Am I misusing the match_results class in
some way?

Thanks for any help and best regards
Pascal


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net