firmo23
firmo23

Reputation: 8404

Delete part of string based on a pattern

I would like to know how could I find a pattern to delete certain part of every string in a vector.

stri<-c("protein mono-ADP-ribosylation (GO:0140289)","negative regulation of viral life cycle (GO:1903901)","viral life cycle (GO:1901)")

More specifically everything inside the parenthesis and the parentheses of course.

Upvotes: 1

Views: 56

Answers (3)

Mike V
Mike V

Reputation: 1364

Another way you can try

library(stringr)
str_squish(str_replace(stri, "\\(.*\\)", ""))
# [1] "protein mono-ADP-ribosylation"           "negative regulation of viral life cycle"
# [3] "viral life cycle" 
  • \\(.*\\): matches parenthesis and everything inside.
  • str_squish: reduces repeated whitespace inside a string.

Upvotes: 1

hello_friend
hello_friend

Reputation: 5788

gsub("\\s*\\(.*\\)\\s*", "", stri)

Upvotes: 1

arg0naut91
arg0naut91

Reputation: 14764

You could try:

gsub('\\s\\(.*', '', stri)

Output:

[1] "protein mono-ADP-ribosylation"           "negative regulation of viral life cycle" "viral life cycle" 

Upvotes: 2

Related Questions