Re: [R] Regexp pattern but fixed replacement?

2024-05-23 Thread Enrico Schumann
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, >

Re: [R] Regexp pattern but fixed replacement?

2024-04-11 Thread Duncan Murdoch
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:

Re: [R] Regexp pattern but fixed replacement?

2024-04-11 Thread Duncan Murdoch
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

Re: [R] Regexp pattern but fixed replacement?

2024-04-11 Thread Iris Simmons
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:

Re: [R] Regexp pattern but fixed replacement?

2024-04-11 Thread Dave Dixon
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,