regex - Find and replace entire value in R -


i'm looking way use find , replace function in r replace entire value of string, rather matching part of string. have dataset lot of (very) long names, , i'm looking efficient way find , change values.

so, instance, tried change entire string

string <- "generally.speaking..do.you.prefer.to.try.out.new.experiences.like.trying.things.and.meeting.new.people..or.do.you.prefer.familiar.situations.and.faces." 

to

"exp" 

with code

string <- gsub("experiences", "exp", string) 

however, results in substituting "exp" part of string matches "experiences", , leaves rest of long name intact (bolded clarity):

"generally.speaking..do.you.prefer.to.try.out.new.exp..like.trying.things.and.meeting.new.people..or.do.you.prefer.familiar.situations.and.faces."

in case, because string contains "experiences", should replaced "exp."

is there way tell gsub or other function replace entire value? looked lot of tutorials , seems functions operate within string or on whole values, not between two.

you can use gsub follows:

 gsub(".*experiences.*", "exp", string, perl=true)   # @rawr notes, set perl=true improved efficiency 

this regex matches strings have characters 0 or more times (i.e. .*) followed "experiences", followed characters 0 or more times.

in case, still replacing entire match "exp" using regex, expand definition of match (from "experience" ".*experience.*") achieve desired substitution.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -