Home > Software engineering >  Regex-- How to extract the text after the second hyphen for each parenthesis?
Regex-- How to extract the text after the second hyphen for each parenthesis?

Time:10-21

I am new to the Regex and the grammar made me confused. Here is the original string field : (f-dqcn-bus1),(f-cdqc-bus2)

I would like to have the new result like bus1,bus2.

There could be one or several parenthesis but the bus name is always after the second hyphen in each parenthesis. I plan to use the str.findall(regex_pattern) to extract, and could anyone help to develop the regex_pattern here? Thanks very much!

CodePudding user response:

I'm not sure if this works for you but I've tried it before and I kinda love .str methods in Pandas!

import pandas as pd

data = [('f-dqcn-bus1'), ('f-cdqc-bus2')]
df = pd.DataFrame(data, columns=['column_name'])
tuple(df['column_name'].str.split('-', n = 2, expand = True)[2])

# ('bus1', 'bus2')

CodePudding user response:

Match words that are followed by a close parenthesis:

businesses = re.findall(r"\w (?=\))", str)

See live demo.

  • Related