Home > other >  Regex to extract characters that occur within parenthesis after a string, and the string occurs mult
Regex to extract characters that occur within parenthesis after a string, and the string occurs mult

Time:03-06

This is a very complex regex question. I have been trying to achieve this for the past few hours and I thought it would be better to seek help, and also add value to the community by having a reference solution to this complex problem.

I have a column full of string documents. The goal is to extract the Ticker symbol that appears within parenthesis after the keyword: "Executive Decision" (not exactly after this keyword). There is more than one occurrence of this keyword in the strings.

The goal is to extract such ticker symbol within () that appear after a few words following the keyword "Executive Decision" from the string and store them into a list that gets added to the row of a new column.

My sample string:

"Search Jim Cramer's ""Mad Money"" trading recommendations using our exclusive ""Mad Money"" Stock Screener. ""We're not in Kansas anymore, we're in Oz,"" Jim Cramer told his Mad Money viewers Tuesday after another wild day on Wall Street. Cramer said the market's gyrations and confusion are becoming par for the course and investors need to get used to it. There's a palpable sense of gloom in the markets, Cramer noted, with many hedge fund managers taking profits and locking in gains, while individual investors follow them out the door. But Cramer said while he doesn't share in the depth of the despair he remains cautious. The markets continue to fret over the direction of interest rates, with the Federal Reserve set to make its next move on Sept. 21. The Fed's comments have been all over the place, Cramer noted, so much so that investors might need trading cards to keep all of the names and faces straight. Some Fed officials are pounding the table for immediate rate hikes, while others remain dovish and still others are ambiguous at best. Then there's the price of oil, which is up big one day, down big the next. No one seems to know if inventories and subsequently demand is rising or falling. Cramer believes that stocks are ridiculously cheap given our economy's low inflation and unemployment, coupled with the record earnings many companies have been able to produce. That's why he continues to raise cash for his charitable trust, Action Alerts PLUS, so he can pounce on these value names as the market takes them lower. Off the Charts In the ""Off the Charts"" segment, Cramer checked in with colleague Mark Sebastian to see where the markets might be headed next as indicated by the CBOE Volatility IndexI:VIX , better known by its ticker symbol, the VIX. Looking at a chart of the past year, Sebastian noted that back in December when the Fed last raised interest rates, the VIX saw a big spike that continued throughout January and February. The VIX also tends to spike just ahead of the release of the non-farm payroll numbers. But over the past six weeks, Sebastian saw a different pattern, one where the VIX is rising along with the S&P 500. That's a warning sign and a clear signal more choppiness is ahead. Cramer concurred with Sebastian, saying the charts indicate the turbulence is far from over. Only when the VIX begins to fall, he said, will the fear actually be baked into stock prices. In the meantime, Cramer said it's not worth the risk to try and be a hero. Executive Decision: John Stumpf For his ""Executive Decision"" segment, Cramer sat down with John Stumpf, chairman and CEO of Wells Fargo (WFC) - Get Wells Fargo & Company Report , a bank that's come under fire for predatory practices. Stumpf apologized to Wells Fargo customers who found themselves with products they did not want. He said Wells is built on a culture of doing right by customers and he personally takes responsibility for any case where that did not happen. Stumpf continued that at any given time Wells has 100,000 employees in its banking centers and over the past five years, his bank has fired about 1,000 employees a year for not abiding by their rules. He said a recent audit showed that out of 93 million new accounts, about two million accounts, most of which originated from third parties, could not be fully verified. Out of those, the company identified 155,000 accounts that were not verified and immediately returned $2.6 million in fees from those accounts. Wells Fargo also took the step of removing product sales goals from their retail plan, a move which Stumpf said shouldn't have a material impact on earnings going forward, but will help to remove any confusion about what's expected from their retail staff. TheStreet Recommends INVESTING Two of the Worst Airlines Are Merging and Will Upcharge You FEB 7, 2022 3:47 PM EST INVESTING Elon Musk Jet-Tracking Teen Turns His Sights on Russian Oligarchs 51 MINUTES AGO TECHNOLOGY Elon Musk and Tesla Set Their Sights on a New Industry FEB 27, 2022 6:34 PM EST Stumpf also commented on Senior Vice President Carrie Tolstedt's departure from the company, saying that Wells Fargo does have board procedures in place to reclaim any bonuses or compensation that was not warranted or deemed not appropriate. Stumpf said Wells Fargo remains a growth company and will double down on investments in tools and training to help them achieve their goals of doing right by customers. Fasten Your Seatbelts If there's anything the stock market doesn't like, it's uncertainty, Cramer told viewers, and given the great political divide in our country, investors should be prepared for a rough couple of months. No matter who wins the presidency, there are a lot of questions that remain unanswered. Donald Trump seems to be favoring a trade war with China, and that would be bad for everything from machinery to restaurants to airlines and more. Meanwhile, Hillary Clinton would certainly continue her attacks on healthcare and pharma while at the same time possibly raise capital gains taxes. Perhaps the only common ground among the candidates is defense, Cramer noted, but those stocks have already run up big ahead of the election. That's why the only certain thing investors can expect is more uncertainty. Lightning Round In the Lightning Round, Cramer was bullish on Ellie Mae (ELLI) and Total System Service (TSS) - Get Total System Services, Inc. Report . Cramer was bearish on Penske Automotive Group (PAG) - Get Penske Automotive Group, Inc. Report and Silver Run Acquisition (SRAQU) . Executive Decision: Michael Mahoney In his second ""Executive Decision"" segment, Cramer sat down with Michael Mahoney, chairman and CEO of Boston Scientific (BSX) - Get Boston Scientific Corporation Report , the medical device maker with a stock that's up almost 30% for 2016. Mahoney said Boston Scientific has over 25,000 employees around the globe focused on innovation and there is a lot more growth to come. He said his company's Synergy stents are taking share globally, while the new Lotus heart valve replacement is seeing excellent clinical outcomes. Boston Scientific continues to innovate with its endoscopy products, making those procedures less invasive for patients. The company's pacemakers have twice the warranty length of competitors. Mahoney said his company is also making strides to combat chronic pain, which affects over 100 million people. Finally, Mahoney said the clinical trials for his company's treatment to manage Parkinson's disease is also progressing well and he expects to file for approval in late-2017. Cramer said when the markets are down on interest rate worries, this is the stock to buy. To watch replays of Cramer's video segments, visit the Mad Money page on CNBC. To sign up for Jim Cramer's free Booyah! newsletter with all of his latest articles and videos please click here. At the time of publication, Cramer's Action Alerts PLUS had a position in WFC. MAD MONEYJIM CRAMEROPINIONSTOCKS BY SCOTT RUTT Scott Rutt is a veteran staff writer at TheStreet, having covered Jim Cramer and Mad Money for over 14 years."

Desired output from the above string: [WFC, BSX]

CodePudding user response:

You might want to do non-greedy search as follows:

import re
s  = "Search Jim Cramer's ""Mad Money"" trading recommendations using our exclusive ""Mad Money"" Stock Screener. ""We're not in Kansas anymore, we're in Oz,"" Jim Cramer told his Mad Money viewers Tuesday after another wild day on Wall Street. Cramer said the market's gyrations and confusion are becoming par for the course and investors need to get used to it. There's a palpable sense of gloom in the markets, Cramer noted, with many hedge fund managers taking profits and locking in gains, while individual investors follow them out the door. But Cramer said while he doesn't share in the depth of the despair he remains cautious. The markets continue to fret over the direction of interest rates, with the Federal Reserve set to make its next move on Sept. 21. The Fed's comments have been all over the place, Cramer noted, so much so that investors might need trading cards to keep all of the names and faces straight. Some Fed officials are pounding the table for immediate rate hikes, while others remain dovish and still others are ambiguous at best. Then there's the price of oil, which is up big one day, down big the next. No one seems to know if inventories and subsequently demand is rising or falling. Cramer believes that stocks are ridiculously cheap given our economy's low inflation and unemployment, coupled with the record earnings many companies have been able to produce. That's why he continues to raise cash for his charitable trust, Action Alerts PLUS, so he can pounce on these value names as the market takes them lower. Off the Charts In the ""Off the Charts"" segment, Cramer checked in with colleague Mark Sebastian to see where the markets might be headed next as indicated by the CBOE Volatility IndexI:VIX , better known by its ticker symbol, the VIX. Looking at a chart of the past year, Sebastian noted that back in December when the Fed last raised interest rates, the VIX saw a big spike that continued throughout January and February. The VIX also tends to spike just ahead of the release of the non-farm payroll numbers. But over the past six weeks, Sebastian saw a different pattern, one where the VIX is rising along with the S&P 500. That's a warning sign and a clear signal more choppiness is ahead. Cramer concurred with Sebastian, saying the charts indicate the turbulence is far from over. Only when the VIX begins to fall, he said, will the fear actually be baked into stock prices. In the meantime, Cramer said it's not worth the risk to try and be a hero. Executive Decision: John Stumpf For his ""Executive Decision"" segment, Cramer sat down with John Stumpf, chairman and CEO of Wells Fargo (WFC) - Get Wells Fargo & Company Report , a bank that's come under fire for predatory practices. Stumpf apologized to Wells Fargo customers who found themselves with products they did not want. He said Wells is built on a culture of doing right by customers and he personally takes responsibility for any case where that did not happen. Stumpf continued that at any given time Wells has 100,000 employees in its banking centers and over the past five years, his bank has fired about 1,000 employees a year for not abiding by their rules. He said a recent audit showed that out of 93 million new accounts, about two million accounts, most of which originated from third parties, could not be fully verified. Out of those, the company identified 155,000 accounts that were not verified and immediately returned $2.6 million in fees from those accounts. Wells Fargo also took the step of removing product sales goals from their retail plan, a move which Stumpf said shouldn't have a material impact on earnings going forward, but will help to remove any confusion about what's expected from their retail staff. TheStreet Recommends INVESTING Two of the Worst Airlines Are Merging and Will Upcharge You FEB 7, 2022 3:47 PM EST INVESTING Elon Musk Jet-Tracking Teen Turns His Sights on Russian Oligarchs 51 MINUTES AGO TECHNOLOGY Elon Musk and Tesla Set Their Sights on a New Industry FEB 27, 2022 6:34 PM EST Stumpf also commented on Senior Vice President Carrie Tolstedt's departure from the company, saying that Wells Fargo does have board procedures in place to reclaim any bonuses or compensation that was not warranted or deemed not appropriate. Stumpf said Wells Fargo remains a growth company and will double down on investments in tools and training to help them achieve their goals of doing right by customers. Fasten Your Seatbelts If there's anything the stock market doesn't like, it's uncertainty, Cramer told viewers, and given the great political divide in our country, investors should be prepared for a rough couple of months. No matter who wins the presidency, there are a lot of questions that remain unanswered. Donald Trump seems to be favoring a trade war with China, and that would be bad for everything from machinery to restaurants to airlines and more. Meanwhile, Hillary Clinton would certainly continue her attacks on healthcare and pharma while at the same time possibly raise capital gains taxes. Perhaps the only common ground among the candidates is defense, Cramer noted, but those stocks have already run up big ahead of the election. That's why the only certain thing investors can expect is more uncertainty. Lightning Round In the Lightning Round, Cramer was bullish on Ellie Mae (ELLI) and Total System Service (TSS) - Get Total System Services, Inc. Report . Cramer was bearish on Penske Automotive Group (PAG) - Get Penske Automotive Group, Inc. Report and Silver Run Acquisition (SRAQU) . Executive Decision: Michael Mahoney In his second ""Executive Decision"" segment, Cramer sat down with Michael Mahoney, chairman and CEO of Boston Scientific (BSX) - Get Boston Scientific Corporation Report , the medical device maker with a stock that's up almost 30% for 2016. Mahoney said Boston Scientific has over 25,000 employees around the globe focused on innovation and there is a lot more growth to come. He said his company's Synergy stents are taking share globally, while the new Lotus heart valve replacement is seeing excellent clinical outcomes. Boston Scientific continues to innovate with its endoscopy products, making those procedures less invasive for patients. The company's pacemakers have twice the warranty length of competitors. Mahoney said his company is also making strides to combat chronic pain, which affects over 100 million people. Finally, Mahoney said the clinical trials for his company's treatment to manage Parkinson's disease is also progressing well and he expects to file for approval in late-2017. Cramer said when the markets are down on interest rate worries, this is the stock to buy. To watch replays of Cramer's video segments, visit the Mad Money page on CNBC. To sign up for Jim Cramer's free Booyah! newsletter with all of his latest articles and videos please click here. At the time of publication, Cramer's Action Alerts PLUS had a position in WFC. MAD MONEYJIM CRAMEROPINIONSTOCKS BY SCOTT RUTT Scott Rutt is a veteran staff writer at TheStreet, having covered Jim Cramer and Mad Money for over 14 years."
results = re.findall(r'Executive Decision.*?\((.*?)\)', s)
assert results == ['WFC', 'BSX']

[EDIT]

(Comment to the OP's answer) You don't even need to use the apply method when it comes to pandas DataFrame.

import pandas as pd

# Create a sample dataframe
df = pd.DataFrame({'transcript': ["Mad Money"" trading recommendations using our exclusive ""Mad Money"" Stock Screener. ""We're not in Kansas anymore, we're in Oz,"" Jim Cramer told his Mad Money viewers Tuesday after another wild day on Wall Street. Cramer said the market's gyrations and confusion are becoming par for the course and investors need to get used to it. There's a palpable sense of gloom in the markets, Cramer noted, with many hedge fund managers taking profits and locking in gains, while individual investors follow them out the door. But Cramer said while he doesn't share in the depth of the despair he remains cautious. The markets continue to fret over the direction of interest rates, with the Federal Reserve set to make its next move on Sept. 21. The Fed's comments have been all over the place, Cramer noted, so much so that investors might need trading cards to keep all of the names and faces straight. Some Fed officials are pounding the table for immediate rate hikes, while others remain dovish and still others are ambiguous at best. Then there's the price of oil, which is up big one day, down big the next. No one seems to know if inventories and subsequently demand is rising or falling. Cramer believes that stocks are ridiculously cheap given our economy's low inflation and unemployment, coupled with the record earnings many companies have been able to produce. That's why he continues to raise cash for his charitable trust, Action Alerts PLUS, so he can pounce on these value names as the market takes them lower. Off the Charts In the ""Off the Charts"" segment, Cramer checked in with colleague Mark Sebastian to see where the markets might be headed next as indicated by the CBOE Volatility IndexI:VIX , better known by its ticker symbol, the VIX. Looking at a chart of the past year, Sebastian noted that back in December when the Fed last raised interest rates, the VIX saw a big spike that continued throughout January and February. The VIX also tends to spike just ahead of the release of the non-farm payroll numbers. But over the past six weeks, Sebastian saw a different pattern, one where the VIX is rising along with the S&P 500. That's a warning sign and a clear signal more choppiness is ahead. Cramer concurred with Sebastian, saying the charts indicate the turbulence is far from over. Only when the VIX begins to fall, he said, will the fear actually be baked into stock prices. In the meantime, Cramer said it's not worth the risk to try and be a hero. Executive Decision: John Stumpf For his ""Executive Decision"" segment, Cramer sat down with John Stumpf, chairman and CEO of Wells Fargo (WFC) - Get Wells Fargo & Company Report , a bank that's come under fire for predatory practices. Stumpf apologized to Wells Fargo customers who found themselves with products they did not want. He said Wells is built on a culture of doing right by customers and he personally takes responsibility for any case where that did not happen. Stumpf continued that at any given time Wells has 100,000 employees in its banking centers and over the past five years, his bank has fired about 1,000 employees a year for not abiding by their rules. He said a recent audit showed that out of 93 million new accounts, about two million accounts, most of which originated from third parties, could not be fully verified. Out of those, the company identified 155,000 accounts that were not verified and immediately returned $2.6 million in fees from those accounts. Wells Fargo also took the step of removing product sales goals from their retail plan, a move which Stumpf said shouldn't have a material impact on earnings going forward, but will help to remove any confusion about what's expected from their retail staff. TheStreet Recommends INVESTING Two of the Worst Airlines Are Merging and Will Upcharge You FEB 7, 2022 3:47 PM EST INVESTING Elon Musk Jet-Tracking Teen Turns His Sights on Russian Oligarchs 51 MINUTES AGO TECHNOLOGY Elon Musk and Tesla Set Their Sights on a New Industry FEB 27, 2022 6:34 PM EST Stumpf also commented on Senior Vice President Carrie Tolstedt's departure from the company, saying that Wells Fargo does have board procedures in place to reclaim any bonuses or compensation that was not warranted or deemed not appropriate. Stumpf said Wells Fargo remains a growth company and will double down on investments in tools and training to help them achieve their goals of doing right by customers. Fasten Your Seatbelts If there's anything the stock market doesn't like, it's uncertainty, Cramer told viewers, and given the great political divide in our country, investors should be prepared for a rough couple of months. No matter who wins the presidency, there are a lot of questions that remain unanswered. Donald Trump seems to be favoring a trade war with China, and that would be bad for everything from machinery to restaurants to airlines and more. Meanwhile, Hillary Clinton would certainly continue her attacks on healthcare and pharma while at the same time possibly raise capital gains taxes. Perhaps the only common ground among the candidates is defense, Cramer noted, but those stocks have already run up big ahead of the election. That's why the only certain thing investors can expect is more uncertainty. Lightning Round In the Lightning Round, Cramer was bullish on Ellie Mae (ELLI) and Total System Service (TSS) - Get Total System Services, Inc. Report . Cramer was bearish on Penske Automotive Group (PAG) - Get Penske Automotive Group, Inc. Report and Silver Run Acquisition (SRAQU) . Executive Decision: Michael Mahoney In his second ""Executive Decision"" segment, Cramer sat down with Michael Mahoney, chairman and CEO of Boston Scientific (BSX) - Get Boston Scientific Corporation Report , the medical device maker with a stock that's up almost 30% for 2016. Mahoney said Boston Scientific has over 25,000 employees around the globe focused on innovation and there is a lot more growth to come. He said his company's Synergy stents are taking share globally, while the new Lotus heart valve replacement is seeing excellent clinical outcomes. Boston Scientific continues to innovate with its endoscopy products, making those procedures less invasive for patients. The company's pacemakers have twice the warranty length of competitors. Mahoney said his company is also making strides to combat chronic pain, which affects over 100 million people. Finally, Mahoney said the clinical trials for his company's treatment to manage Parkinson's disease is also progressing well and he expects to file for approval in late-2017. Cramer said when the markets are down on interest rate worries, this is the stock to buy. To watch replays of Cramer's video segments, visit the Mad Money page on CNBC. To sign up for Jim Cramer's free Booyah! newsletter with all of his latest articles and videos please click here. At the time of publication, Cramer's Action Alerts PLUS had a position in WFC. MAD MONEYJIM CRAMEROPINIONSTOCKS BY SCOTT RUTT Scott Rutt is a veteran staff writer at TheStreet, having covered Jim Cramer and Mad Money for over 14 years."]})

# Extract the target elements
df['interview_ticker'] = df['transcript'].str.findall(r'Executive Decision.*?\((.*?)\)')

CodePudding user response:

df['interview_ticker']= df['transcript'].apply(lambda x: re.findall(r'Executive Decision.*?\((.*?)\)', x))

Pandas implementation. Credits to @quasi-human

  • Related