On Thu, 11 Apr 2024, Duncan Murdoch writes:
> I noticed this issue in stringr::str_replace, but it
> also affects sub() in base R.
>
> If the pattern in a call to one of these needs to be a
> regular expression, then backslashes in the replacement
> text are treated specially.
>
> For example,
>
On 11/04/2024 12:58 p.m., Iris Simmons wrote:
Hi Duncan,
I only know about sub() and gsub().
There is no way to have pattern be a regular expression and replacement
be a fixed string.
Backslash is the only special character in replacement. If you need a
reference, see this file:
On 11/04/2024 12:57 p.m., Dave Dixon wrote:
Backslashes in regex expressions in R are maddening, but they make sense.
R string handling interprets your replacement string "\\" as just one
backslash. Your string is received by gsub as "\" - that is, just the
control backslash, NOT the character
Hi Duncan,
I only know about sub() and gsub().
There is no way to have pattern be a regular expression and replacement be
a fixed string.
Backslash is the only special character in replacement. If you need a
reference, see this file:
Backslashes in regex expressions in R are maddening, but they make sense.
R string handling interprets your replacement string "\\" as just one
backslash. Your string is received by gsub as "\" - that is, just the
control backslash, NOT the character backslash. gsub is expecting to see
\0,
5 matches
Mail list logo