Re: [Regex] \r matching \n
Rainer Deyke via Boost <[email protected]>
| Newsgroups | gmane.comp.lib.boost.devel |
|---|---|
| Message-ID | <[email protected]> |
On 3/25/26 10:22, Olaf van der Spek via Boost wrote: > On Fri, Mar 20, 2026 at 8:34 PM John Maddock via Boost > <[email protected]> wrote: >> >> On 20/03/2026 07:29, Olaf van der Spek via Boost wrote: >>> 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" > > Hi John, > > Did you mean R"(\\n)" ? > Or "\\n" ? According to documentation (https://www.boost.org/doc/libs/latest/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html): R"( )" = "\n" should match itself, like all characters not in .[{}()\*+?|^$ R"(\n)" = "\\n" should match exactly '\n' and not '\r', just like "\n". The documentation is explicit about this. R"(\r)" = "\\r" should match '\r', as should "\r", for what it's worth. R"(\\n)" = "\\n" should match "\\n", i.e. a backslash followed by 'n'. '$' should match the end of a line, including embedded newlines in the text. It is not clear what qualifies as a newline in this sense, but I'm guessing '\r' might qualify. Any other behavior is either an error in the documentation or a bug in the code. -- Rainer Deyke - [email protected] _______________________________________________ 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/D7T6KNWVZOQ6EG5RETP5LO4VRRGX4ERR/