S
S
Sergey Beresnev2012-02-14 12:35:27
PHP
Sergey Beresnev, 2012-02-14 12:35:27

Need help building a regular expression?

There is a string
string|name1,value1|name2,value2|name3,value3|...
I really want to make one regular expression so that I can pull out all the necessary values: string, name1, value1, name2, value2, name3, value3 and so on. I really want to make it myself, but I can't.
string - can be a more complex expression that will need to be parsed in the same regular expression.
UPD. An example would be an address like this: www.google.ru/?sclient=psy-ab&hl=ru&safe=off&sourc...
where string is www.google.ru/? and the rest of the parameters. The regular is needed so that it can be passed as an argument.

Answer the question

In order to leave comments, you need to log in

7 answer(s)
D
deadkrolik, 2012-02-14
@deadkrolik

$a = "string|name1,value1|name2,value2|name3,value3|...";
preg_match_all("/([^|,]+)/",$a,$matches);
var_dump($matches);

S
Stdit, 2012-02-14
@Stdit

preg_match_all("~([^|,]+)\|([^,]+)~","string|name1,value1|name2,value2|name3",$m);var_dump($m);

  [1]=>
  array(3) {
    [0]=>
    string(6) "string"
    [1]=>
    string(6) "value1"
    [2]=>
    string(6) "value2"
  }
  [2]=>
  array(3) {
    [0]=>
    string(5) "name1"
    [1]=>
    string(5) "name2"
    [2]=>
    string(5) "name3"
  }

P
pel, 2012-02-14
@pel

Do you need a regular only to have less code? Maybe do without them?

<?php

$s = 'string|name1,value1|name2,value2|name3,value3';
$b = array();

foreach( explode('|', $s) AS $e ) {
  list($k, $v) = explode(',', $e);
  $b[$k] = $v;
}

?>

$b: Array
(
    [string] => 
    [name1] => value1
    [name2] => value2
    [name3] => value3
)

Only about “string” it is not entirely clear what you meant ... Can you give an example?

T
theRavel, 2012-02-14
@theRavel

// Вот регулярка вашей мечты:
$pattern = '/^(?<prefix>\w+)|(?<names>\w+),(?<values>\w+)/i';
$value = 'string|name1,value1|name2,value2';
preg_match_all($pattern, $value, $matches);
var_dump($matches);

// Можно почистить лишние матчеры
array_walk($matches, function(&$item, $key) use (&$matches) {
   if (is_numeric($key)) {
      unset($matches[$key]);
      return;
   }
   foreach ($item as $key => $match) {
      if (empty($match)) {
         unset($item[$key]);
      }
   }
});
var_dump($matches);

K
Kuzma, 2012-02-14
@Kuzma

Not sure if I understood the problem correctly. The wording is too confusing.
<?php
$string = 'string|name1,value1|name2,value2|name3,value3';
$regexp = '/[a-zA-Z0-9]+/';
preg_match_all($regexp,$string,$matches);
var_dump($matches);

T
the_ghost, 2012-02-14
@the_ghost

What about word boundary - \b ?
ideone.com/w5cgt
<?
$str = 'string|name1,value1|name2,value2|name3,value3|';
preg_match_all('#\b(\w+)\b#', $str, $matches);
var_dump($matches);
?>

[0] => Array
(
[0] => string
[1] => name1
[2] => value1
[3] => name2
[4] => value2
[5] => name3
[6] => value3
)

V
Viktor Kuznetsov, 2012-02-14
@janitor

(\w+)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question