Re: [boost] [Regex] Why doesn't this pattern match?

Yes, Robert, You are right. I am trying to change \\n (wchar_t '\' and wchar_t 'n') with a real new line symbols and it doesn't work. Allthought, then I am trying to find only wchar_t 'n' (or any other letter) everything is working good. The problem is that I have XML configuration file (Unicode), where all new lines are L"\\n" (!!! not '\n' !!!). I can't change that file format. Of course I can program that replace without boost::regex, but I am afraid that I'll loose my sleep afterwards...
You're replacing each occurrence of \\n with \\n, so the string at the end should be exactly the same! :-)
The replacement is "\n" not "\\n" so the '\' followed by 'n' in str should be replaced with a newline, right?

Yes, Robert, You are right. I am trying to change \\n (wchar_t '\' and wchar_t 'n') with a real new line symbols and it doesn't work. Allthought, then I am trying to find only wchar_t 'n' (or any other letter) everything is working good. The problem is that I have XML configuration file (Unicode), where all new lines are L"\\n" (!!! not '\n' !!!). I can't change that file format. Of course I can program that replace without boost::regex, but I am afraid that I'll loose my sleep afterwards...
You're replacing each occurrence of \\n with \\n, so the string at the end should be exactly the same! :-)
The replacement is "\n" not "\\n" so the '\' followed by 'n' in str should be replaced with a newline, right?
No. Remember the compiler gets first bite at the escape characters, so if you put a string "\\n" in your program, that gets passed to Boost.Regex as the two character string: "\n", which matches a single newline character. If you want to match a \ followed by a \n then you need a regex of "\\\n". HTH, John.

Yes, Robert, You are right. I am trying to change \\n (wchar_t '\' and wchar_t 'n') with a real new line symbols and it doesn't work. Allthought, then I am trying to find only wchar_t 'n' (or any other letter) everything is working good. The problem is that I have XML configuration file (Unicode), where all new lines are L"\\n" (!!! not '\n' !!!). I can't
change that file format. Of course I can program that replace without
boost::regex, but I am afraid that I'll loose my sleep afterwards...
You're replacing each occurrence of \\n with \\n, so the string at the end should be exactly the same! :-)
The replacement is "\n" not "\\n" so the '\' followed by 'n' in str should be replaced with a newline, right?
No. Remember the compiler gets first bite at the escape characters, so if you put a string "\\n" in your program, that gets passed to Boost.Regex as the two character string: "\n", which matches a single newline character. If you want to match a \ followed by a \n then you need a regex of "\\\n".
If you want to match a '\' followed by an 'n' (as I think the OP needs), do you need a regex of "\\\\n" (or "\\Q\\n\\E")? ************************************************************************ The information contained in this message or any of its attachments may be confidential and is intended for the exclusive use of the addressee(s). Any disclosure, reproduction, distribution or other dissemination or use of this communication is strictly prohibited without the express permission of the sender. The views expressed in this email are those of the individual and not necessarily those of Sony or Sony affiliated companies. Sony email is for business use only. This email and any response may be monitored by Sony to be in compliance with Sony's global policies and standards

Didn't knew regex engine is working in that way... If he understands "\\\\n" as C++ "\\n" and "\\n" as C++ understands "\n", then how regex engine understands "\n" ...? It's nothing to do with how the regex engine works; it's the fact that backslashes are interpreted as escape sequences in C++ as well as in Regex. So, if you include "\\\\n" as a regex in your C++ source, the regex engine is actually passed "\\n"; if you were to read the regex
On Tue, Dec 8, 2009 at 6:07 AM, Vitalij Gotovskij <Vitalij.gotovskij@ashburn.lt> wrote: pattern from a file or the console, you'd use "\\n" rather than "\\\\n" for the effect you want. Similarly, if you include "\\n" as a regex in your C++ source, the regex engine actually sees "\n" and hence matches a newline. If you read the regex pattern from a file or the console, you'd need to enter "\n" rather than "\\n". And finally, if you include "\n" as a regex in your C++ source, the regex engine actually sees a newline character rather than the escape sequence "\n". I'm not really sure if it will then match a newline character; I would assume so, though. On Tue, Dec 8, 2009 at 6:10 AM, Sylvester-Bradley, Gareth <Gareth.Sylvester-Bradley@eu.sony.com> wrote:
If you want to match a '\' followed by an 'n' (as I think the OP needs), do you need a regex of "\\\\n" (or "\\Q\\n\\E")?
Indeed. After compilation, "\\\\n" becomes "\\n", which matches '\' followed by 'n'. --CM
participants (4)
-
Celtic Minstrel
-
John Maddock
-
Sylvester-Bradley, Gareth
-
Vitalij Gotovskij