[R] Pull Stock Symbol Out of String

arun smartpink111 at yahoo.com
Wed Apr 9 03:17:55 CEST 2014

You may try:
str1 <- c("American Tower Corporation (REIT) (AMT)", "Aetna Inc. (AET)")
#[1] "AMT" "AET" 


On Tuesday, April 8, 2014 7:48 PM, "Sparks, John James" <jspark4 at uic.edu> wrote:
Dear R Helpers,

My regex skills are beginner to intermediate and banging around the web
has not resulted in a solution to the problem below so I hope that one of
you who has mad skills can help me out.

I want to extract the stock ticker--AMT-- out of the string

American Tower Corporation (REIT) (AMT)

The presence of the other parenthetical text (REIT) makes this difficult. 
Please note that the string may or may not have a interfering set of
characters such as the (REIT) so the solution needs to be generalizable to
the last set of characters that are contained in parentheses in the larger
string.  So an example of a string without the interfering (REIT) would be

Aetna Inc. (AET)

Your assistance would be very much appreciated.

--John Sparks

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list