Martyn Ball
Martyn Ball

Reputation: 4885

Regex match string by one delimiter, and then another

im struggling to find an regex which will do what I need.

I just want to first split the following by each pipe, and then split that match be each colon.

partx_registration:DL66 LNH|test:value|test2:helloworld

So I would be left with 3 groups, and 2 values within them 2 groups.

Currently got the following:

/([^|]+)/g

However not too sure on how I would go about doing the second check.

Edit:

Using PHP, and I would expect:

(2) [Array(2), Array(2)]
   0:
   (2) ["partx_registration", "DL66 LNH"]
   1:
   (2) ["test", "value"]

Edit 2:

The following code:

preg_match_all("~([^|:]+):([^|:]+)~", "partx_registration:DL66 LNH|test:value|test2:helloworld", $post_array);
echo "<pre>";
var_dump($post_array);
echo "</pre>";
die();

Outputs:

array(3) {
  [0]=>
  array(3) {
    [0]=>
    string(27) "partx_registration:DL66 LNH"
    [1]=>
    string(10) "test:value"
    [2]=>
    string(16) "test2:helloworld"
  }
  [1]=>
  array(3) {
    [0]=>
    string(18) "partx_registration"
    [1]=>
    string(4) "test"
    [2]=>
    string(5) "test2"
  }
  [2]=>
  array(3) {
    [0]=>
    string(8) "DL66 LNH"
    [1]=>
    string(5) "value"
    [2]=>
    string(10) "helloworld"
  }
}

This isn't the case on regex101 :S

Upvotes: 1

Views: 39

Answers (2)

axiac
axiac

Reputation: 72226

You don't need regex for such a simple task. Split the initial string by | then each piece by : and you're almost there. All you need is explode() and foreach. And the resulting code is more clear than using regex.

$input = 'partx_registration:DL66 LNH|test:value|test2:helloworld';

$output = array();
foreach (explode('|', $input) as $piece) {
    $output[] = explode(':', $piece);
}
print_r($output);

The output is:

Array
(
    [0] => Array
        (
            [0] => partx_registration
            [1] => DL66 LNH
        )

    [1] => Array
        (
            [0] => test
            [1] => value
        )

    [2] => Array
        (
            [0] => test2
            [1] => helloworld
        )
)

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626932

You may use

'~([^|:]+):([^|:]+)~'

See the regex demo

Details

  • ([^|:]+) - Group 1: any one or more chars other than | and :
  • : - a colon
  • ([^|:]+) - Group 2: any one or more chars other than | and :

PHP demo:

$str = 'partx_registration:DL66 LNH|test:value|test2:helloworld';
preg_match_all('/([^|:]+):([^|:]+)/', $str, $matches, PREG_SET_ORDER, 0);
print_r($matches);

Results:

Array
(
    [0] => Array
        (
            [0] => partx_registration:DL66 LNH
            [1] => partx_registration
            [2] => DL66 LNH
        )

    [1] => Array
        (
            [0] => test:value
            [1] => test
            [2] => value
        )

    [2] => Array
        (
            [0] => test2:helloworld
            [1] => test2
            [2] => helloworld
        )
)

Upvotes: 1

Related Questions