Search code examples
regexparentheses

Regex to split text within parentheses at end of line using Java


I need to parse some text that has data enclosed within parentheses at the end of line.

Snarky Spark was at the stage with his team (Jerry Mander/Kodi Player/Bella Bella)

I need to extract the text within parantheses and separated by forward slash in capture groups.

  1. Jerry Mander
  2. Kodi Player
  3. Bella Bella

I have tried the following to split by /

(?:[^\/])+

But not able to split it within parenthesis or as end of line criteria.

Appreciate help


Solution

  • You may use this regex with \G:

    (?:\(|(?!^)\G/)([^/)]+)(?=[^()]*\))
    

    RegEx Demo

    \G asserts position at the end of the previous match or the start of the string for the first match.