regex - Shorten length of character in R -
i have vector of characters this:
sampledata <- c("this see i.r.o not i.r.o", "similar here a.s. a.s.", "one more i.r.o. i.r.o.")
i remove after first occurence of i.r.o
or .i.r.o
. in cases a.s
or a.s.
.
so final version looks this:
1 see i.r.o 2 similar here a.s. 3 1 more i.r.o.
edit: corrected gaps between i.r.o
, a.s
gsub()
expressionas identical in each character. see example above.
i'm little confused because comments above suggest you've gotten answer, don't see it.
this seems work:
sampledata <- c("this see i.r.o not i.r.o", "similar here a.s. a.s.", "one more i.r.o. i.r.o.") gsub("(([[:alpha:]]\\.)+[[:alpha:]][.]?) .*$","\\1",sampledata) ## [1] "this see i.r.o" "similar here a.s." ## [3] "one more i.r.o."
the regex reads "'(one or more of (an alphabetic character followed dot), followed alphabetic character possibly followed dot), followed space , 0 or more of character, followed end of line'; replace stuff in quotation marks stuff within (outer set of) parentheses"
Comments
Post a Comment