views:

27

answers:

1

I'm trying to come up with a regex that constructs an array that looks like the one below, from the following string

$str = 'Hello world [something here]{optional}{optional}{optional}{n possibilities of this}';

So far I have /^(\*{0,3})(.+)\[(.*)\]((?:{[a-z ]+})?)$/

Array
(
    [0] => Array
        (
            [0] => Hello world [something here]{optional}{optional}{optional}{n possibilities of this}
            [1] => 
            [2] => Hello world
            [3] => something here
            [4] => {optional}
            [5] => {optional}
            [6] => {optional}
            [7] => ...
            [8] => ...
            [9] => {n of this}
        )
)

What would be a good approach for this? Thanks

A: 

I think you will need two steps for this.

  1. (.+)\[(.+)\](.+) will get you Hello world, something here, and {optional}...{optional}.

  2. Applying \{(.+?)\} to the last element from the previous step will get you optional params.

serg