Re: [Regex] \r matching \n

John Maddock via Boost <[email protected]>
Newsgroups gmane.comp.lib.boost.devel
Message-ID <[email protected]>
On 21/03/2026 15:31, Rainer Deyke via Boost wrote:
> On 3/20/26 20:33, John Maddock via Boost wrote:
>> On 20/03/2026 07:29, Olaf van der Spek via Boost wrote:
>>> Hi,
>>>
>>> In this regex the \n matches \r. How is this defined / controlled? Is
>>> there a way to only match \n?
>>>
>>>      std::string s = "[*] A\rB\n";
>>>      s = boost::regex_replace(s, boost::regex(R"(\[\*\](.+?)(\n|$))"),
>>> "<li>\\1</li>");
>>>      std::println("{}", js_encode(s)); // <li> A</li>\rB\n
>>
>> \n is a regular expression meaning "Match any newline character", 
>> which happens to include \r and a few other things as well.  If you 
>> want to match a literal '\n' then you need to escape it and the 
>> appropriate regular expression string is therefore R"\\n"
>
> He's trying to match the newline character, not '\' followed by 'n'. 
> Which in a raw string would be R"(
> )", with the actual line break in the raw string.
>
>
Yes exactly, the regular expression which matches a literal \n is \\n, 
otherwise \n on it's own is a regular expression operator and not a literal.

John.

_______________________________________________
Boost mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://lists.boost.org/mailman3/lists/boost.lists.boost.org/
Archived at: https://lists.boost.org/archives/list/[email protected]/message/X7D6IXPAIWWDNV2P2IVTGU4IYPKAFZ3P/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.