Monday 15 February 2010

regex - Remove the letters between two patterns of strings in R -



regex - Remove the letters between two patterns of strings in R -

how can remove letters between 2 specific patterns in r?

for instance

a= "a#g abcdefgtdkfef_jpg>pple"

i remove letters between #g , jpg>

a1="apple"

i tried find function in stringr couldn't

there's no need load bundle operation. can utilize base of operations r function sub. it's used match first occurrence of regular expression.

a <- "a#g abcdefgtdkfef_jpg>pple" sub("#g.*jpg>", "", a) # [1] "apple"

regular look explained:

#g matches "#g" .* matches character except \n (zero or more times) jpg> matches "jpg>"

so here we're removing starting @ #g , including jpg>

in regards comment

i tried find function in stringr couldn't

it's spelled stringr (case-sensitive). utilize str_replace.

library(stringr) str_replace(a, "#g.*jpg>", "") # [1] "apple"

regex r string

No comments:

Post a Comment