Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove characters preceding first instance of a capital letter in string in R

I'm trying to remove all characters preceding the first instance of a capital letter for each string in a vector of strings:

x <- c(" its client Auto Group",  "itself and Phone Company", ", client Large Bank")

I've tried:

sub('.*?[A-Z]', '', x) 

But that returns:

"uto Group"  "hone Company"   "arge Bank"

I need it to return:

"Auto Group"    "Phone Company" "Large Bank"

Any ideas?

Thanks.

like image 281
carozimm Avatar asked Jun 15 '16 17:06

carozimm


1 Answers

You need to use a capturing group with a backreference:

sub("^.*?([A-Z])", "\\1", x)

Here,

  • ^ - start of the string
  • .*? - any 0+ characters as few as possible
  • ([A-Z]) - Capture group 1 capturing an uppercase ASCII letter that will be referenced with \1 in the replacement pattern.

So, what we restore what we captured in the result with a backreference.

like image 136
Wiktor Stribiżew Avatar answered Sep 24 '22 04:09

Wiktor Stribiżew