dimitris_ps
dimitris_ps

Reputation: 5951

Remove specified pattern from string in R

I have a string like the following

s <- "abc a%bc 1.2% 234 1.2 (1.4%)) %3ed"

I would like to remove all "words" which have the %. So the result would be

"abc 234 1.2"

Upvotes: 2

Views: 405

Answers (4)

akuiper
akuiper

Reputation: 215117

You can also use str_extract_all from stringr package:

stringr::str_extract_all(s, "(?<=^|\\s)[^%\\s]+(?=\\s|$)")
[[1]]
[1] "abc" "234" "1.2"

(?<=^|\\s) stands for looking behind either the beginning of the string or a white space;

[^%\\s]+ matches a word that doesn't contain % and white space;

(?=\\s|$) stands for looking ahead of either the end of the string or a white space;

Upvotes: 2

989
989

Reputation: 12935

How about this simple approach using base R:

s <- "abc a%bc 1.2% 234 1.2 (1.4%)) %3ed"
spl <- unlist(strsplit(s, " "))
spl[!grepl("%", spl)]

#[1] "abc" "234" "1.2"

Upvotes: 2

rock321987
rock321987

Reputation: 11042

You can use

> gsub("^\\s+|\\s+$", "", (gsub("\\s+", " " ,gsub("\\s+\\S*%\\S*(?=\\s+|$)", " ",input, perl=TRUE))))
#[1] "abc 234 1.2"

Code Breakdown

gsub("^\\s+|\\s+$", "", (gsub("\\s+", " " ,gsub("\\s+\\S*%\\S*(?=\\s+|$)", " ",input, perl=TRUE))))
                                           <--------------------------------------------------->
                                                     Remove strings with %
                        <------------------------------------------------------------------------>
                        Substitute extra spaces with single space from resultant string obtained from above
<-------------------------------------------------------------------------------------------------->
      Trim initial and final whitespaces from the string obtained from above

Regex Breakdown

\\s+ #Match whitespaces
\\S* #Match all non whitespace character before % if its there
% #Match % literally
\\S* #Match all non whitespace character after % if its there
(?=\\s+|$) #Lookahead to check whether there is a space or end of string after matching word with %

Upvotes: 4

Tomas H
Tomas H

Reputation: 713

you can use this

library(stringr)
s <- "abc a%bc 1.2% 234 1.2 (1.4%)) %3ed"
words<-unlist(str_split(s," "))
ind<-which(is.na(str_locate(unlist(str_split(s," ")),"%")[,1]))
vec<-words[ind]
res<-paste(vec, collapse = ' ')
res

Upvotes: 2

Related Questions