[R] Regexp pattern but fixed replacement?

Iris Simmons |kw@|mmo @end|ng |rom gm@||@com
Thu Apr 11 18:58:11 CEST 2024


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:
https://github.com/wch/r-source/blob/04650eddd6d844963b6d7aac02bd8d13cbf440d4/src/main/grep.c
particularly functions R_pcre_string_adj and wstring_adj. So just double
the backslashes in replacement and you'll be good to go.

On Thu, Apr 11, 2024, 12:36 Duncan Murdoch <murdoch.duncan using gmail.com> wrote:

> 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,
>
>    gsub("a|b", "\\", "abcdef")
>
> gives "def", not "\\\\def" as I wanted.  To get the latter, I need to
> escape the replacement backslashes, e.g.
>
>    gsub("a|b", "\\\\", "abcdef")
>
> which gives "\\\\cdef".
>
> I have two questions:
>
> 1.  Is there a variant on sub or str_replace which allows the pattern to
> be declared as a regular expression, but the replacement to be declared
> as fixed?
>
> 2.  To get what I want, I can double the backslashes in the replacement
> text.  This would do that:
>
>     replacement <- gsub("\\\\", "\\\\\\\\", replacement)
>
> Are there any other special characters to worry about besides backslashes?
>
> Duncan Murdoch
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list