Search code examples
pythonpandasextract

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


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!


Solution

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

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

    See live demo.