Search code examples
phpregextext-parsingpreg-splitdelimited

Split strings by different delimiting character sequences


I have the following strings, that comes with the format country: Cum name, Extra info.

Asia: Asian Cup - Play Offs
Asia: Asian Cup
Asia: World Cup - Qualification - First Stage
Australia: A-League
Belgium: Jupiler League - Championship Group
Brazil: Série A

The problem I have is how to seperate the information per line using regex.

More specific, from the first line I like to export the following information:

[ Asia, Asian Cup, Play Offs ]

From the second the information

[ Asia, Asian Cup ]

and so on.

For the moment I have try the following statement:

^([\w]+\:\s+)[^\-]+(?!\-\s)+

It is not completed and I don't know how to continue with this. My primary issue is that I don't know how to negate a part of the statement.

So, what is the way to solve this issue ?


Solution

  • You can try this regex in PHP:

    /^(\p{Lu}\p{L}*):\h*(.+?)(?:\h-\h(.+))?$/mu
    

    RegEx Demo