Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace exact number of characters in string based on occurrence between delimitors in R

I have text strings like this:

u <- "she goes ~Wha::?~ and he's like ~↑Yeah believe me!~ and she's etc."

What I'd like to do is replace all characters occurring between pairs of ~ delimitors (including the delimitors themselves) by, say, X.

This gsub method replaces the substrings between ~-delimitor pairs with a single X:

gsub("~[^~]+~", "X", u)
[1] "she goes X and he's like X and she's etc."

However, what I'd really like to do is replace each and every single character between the delimitors (and the delimitors themselves) by X. The desired output is this:

"she goes XXXXXXXXX and he's like XXXXXXXXXXXXXXXXXXX and she's etc."

I've been experimenting with nchar, backreference, and paste as follows but the result is incorrect:

gsub("(~[^~]+~)", paste0("X{", nchar("\\1"),"}"), u)
[1] "she goes X{2} and he's like X{2} and she's etc."

Any help is appreciated.

like image 807
Chris Ruehlemann Avatar asked Oct 14 '20 11:10

Chris Ruehlemann


People also ask

What replaces one or many matches with a string?

subn() If you want to replace a string that matches a regular expression (regex) instead of perfect match, use the sub() of the re module. In re. sub() , specify a regex pattern in the first argument, a new string in the second, and a string to be processed in the third.

How do you replace a section of a string in regex?

The \[[^\]]*]\[ matches [ , then any 0+ chars other than ] and then ][ . The (...) forms a capturing group #1, it will remember the value that you will be able to get into the replacement with $1 backreference. [^\]]* matches 0+ chars other than ] and this will be replaced.

Can regex replace characters?

RegEx makes replace ing strings in JavaScript more effective, powerful, and fun. You're not only restricted to exact characters but patterns and multiple replacements at once.

How to replace character of all occurrences in a string in R?

Use gsub () to Replace Character of all Occurrences in a String gsub () is also R Base function used to replace all occurrences of the pattern character with another character in a string. Following is the syntax of gsub () function.

How to replace a character in a string column of R Dataframe?

Following are quick examples of how to replace a character in a string column of R DataFrame. let’s create an R DataFrame and run these examples and explore the output. 2. Using sub () – Replace Character in a String sub () is a R Base function that is used to replace a specified character of first occurrences on a string (vector).

How to replace a part of a string with another string?

It is used to replace a part of a string (character) on a column with another string or a character. You can also use pattern matching. 5. Using str_replace_all () – Replace all Characters in a String Use str_replace_all () method of stringr package to replace all occurrences of a character in a DataFrame column or a string.

How to replace every character in a string in Python?

Given a string str of lowercase alphabets only. The task is to replace every character by some new character. The new character belongs to (small case only) Replacement of str [i] is determined by: str [i] = Character obtained after traversing ascii (str [i]) no. of characters in ‘a-z’ repeatedly after str [i]. Attention reader!


1 Answers

The paste0("X{", nchar("\\1"),"}") code results in X{2} because "\\1" is a string of length 2. \1 is not interpolated as a backreference if you do not use it in a string pattern.

You can use the following solution based on stringr:

> u <- "she goes ~Wha::?~ and he's like ~↑Yeah believe me!~ and she's etc."
> str_replace_all(u, '~[^~]+~', function(x) str_dup("X", nchar(x)))
[1] "she goes XXXXXXXX and he's like XXXXXXXXXXXXXXXXXXX and she's etc."

Upon finding a match with ~[^~]+~, the value is passed to the anonymous function and str_dup creates a string out of X that is the same length as the match value.

like image 99
Wiktor Stribiżew Avatar answered Oct 18 '22 18:10

Wiktor Stribiżew