Regex– How to extract the text after the second hyphen for each parenthesis?

Question:

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!

Asked By: Kathie

||

Answers:

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')
Answered By: Sumaia A

Match chars after a dash and before a bracket, and that are neither dash nor bracket:

businesses = re.findall(r"(?<=-)[^-)]+(?=))", str)

See live demo.

Answered By: Bohemian
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.