|
Prev: Array error
Next: c++
From: consultmac2 on 17 Jan 2008 10:55 I am using C++ in VS 8 with the lovely boost libraries. I am processing a log file where the useful information follows a predictably formatted date/time stamp surrounded by [ ] characters. A literal example of the text that I want to strip off is: [ Tue Jan 15 14:30:59 2008 ] a full example of a typical log line is: [ Tue Jan 15 14:30:59 2008 ] Fast Food in T:\restaurants\london As the result of my call to the boost function "regex_replace", indicating that I want it to replace the match with an empty string, I expect the result to be (in the above example's case): Fast Food in T:\restaurants\london The [ and ] characters have special meaning in regular expressions and would seem to need to be 'escaped' somehow. In my program, I am using the boost function: regex_replace, and tried the following in the argument for the regular expression: ...., regex("\[ .* \] "), ... The compiler gives me warnings that both '[' and ']' are "unrecognized character escape sequence"s, but compiles successfully. However, the result of running the regex_replace with that expression on the string in the above example is: [TueJan1514:30:592008]FastFoodinT:\restaurants\london .... which is NOT what I expected. It is essentially just removing all spaces from the text. Thinking that perhaps the use of the '[' and ']' characters is not functional in this case, I changed the argument for the regular expression to: ...., regex("\x5B .\ \x5D "), ... which provides the hex versions of the ASCII equivalents for the two symbols instead. With that, the compilation warnings go away, but the results are exactly the same as before (strips all blanks spaces from the text). I take another stab and change the regular expression argument to: ...., regex("^ .* \] "), ... and get the one compiler warning about the ']' character, but the results indicate that NOTHING gets matched and removed from the text. I MUST be missing something. Suggestions appreciated. Thanks.
From: Mara Guida on 17 Jan 2008 14:06 consultmac2 wrote: > ..., regex("\[ .* \] "), ... regex("\\[ .* \\]")
From: Anand Hariharan on 17 Jan 2008 23:24 On Jan 17, 1:06 pm, Mara Guida <maragu...(a)gmail.com> wrote: > consultmac2 wrote: > > ..., regex("\[ .* \] "), ... > > regex("\\[ .* \\]") Sadly enough, we'll have to wait until the next version of the standard makes way into popular implementations, to get language support for raw string literals (akin to Python). http://groups.google.com/group/comp.lang.c++/msg/32a7144d59aba13b - Anand
From: Jerry Coffin on 19 Jan 2008 01:34 In article <0a03199b-0eee-4635-913e- 5d3908cf201f(a)c4g2000hsg.googlegroups.com>, barryc.ctr(a)gmail.com says... > I am using C++ in VS 8 with the lovely boost libraries. > > I am processing a log file where the useful information follows a > predictably formatted date/time stamp surrounded by [ ] characters. > A literal example of the text that I want to strip off is: > [ Tue Jan 15 14:30:59 2008 ] > > a full example of a typical log line is: > [ Tue Jan 15 14:30:59 2008 ] Fast Food in T:\restaurants\london > > As the result of my call to the boost function "regex_replace", > indicating that I want it to replace the match with an empty string, I > expect the result to be (in the above example's case): > Fast Food in T:\restaurants\london Unless your data has more variation than you've said, you don't need a regex at all. To create a string without the timestamp, you could do something like: std::string stripped(input, input.find(']')+2, -1); The "+2" part is to skip across the ']' and the space that follows. -- Later, Jerry. The universe is a figment of its own imagination.
|
Pages: 1 Prev: Array error Next: c++ |