tags:

views:

59

answers:

1

I have the following code:

preg_match_all('/(.*) \((\d+)\) - ([\d\.\d]+)[,?]/U',
    "E-Book What I Didn't Learn At School... (2) - 3525.01, FREE Intro DVD/Vid (1) - 0.15",
    $match);
var_dump($string, $match);

and get the following ouput:

array(4) {
  [0]=>
  array(1) {
    [0]=>
    string(54) "E-Book What I Didn't Learn At School... (2) - 3525.01,"
  }
  [1]=>
  array(1) {
    [0]=>
    string(39) "E-Book What I Didn't Learn At School..."
  }
  [2]=>
  array(1) {
    [0]=>
    string(1) "2"
  }
  [3]=>
  array(1) {
    [0]=>
    string(7) "3525.01"
  }
}

which matches only one items... what i need is to get all items from such strings. when i've added "," sign to the end of the string - it worked fine. but that is non-sense in adding comma to each string. Any advice?

+4  A: 

Try this regex:

(.*?)\s*\((\d+)\)\s*-\s*(\d+\.\d+)(?:,\s*)?

The major difference is that you had .* (greedy) which I replaced with .*? (un-greedy). Yours first "ate" the entire string (except line breaks) and then back tracked to match just one piece from your string.

Demo:

preg_match_all('/(.*?)\s*\((\d+)\)\s*-\s*(\d+\.\d+)(?:,\s*)?/',
    "E-Book What I Didn't Learn At School... (2) - 3525.01, FREE Intro DVD/Vid (1) - 0.15",
    $matches, PREG_SET_ORDER);
print_r($matches);

produces:

Array
(
    [0] => Array
        (
            [0] => E-Book What I Didn't Learn At School... (2) - 3525.01, 
            [1] => E-Book What I Didn't Learn At School...
            [2] => 2
            [3] => 3525.01
        )

    [1] => Array
        (
            [0] => FREE Intro DVD/Vid (1) - 0.15
            [1] => FREE Intro DVD/Vid
            [2] => 1
            [3] => 0.15
        )

)
Bart Kiers
But what to do with comma? look at the second item
nefo_x
@nefo_x, see the edit.
Bart Kiers
@nefo_x was using the `/U` modifier, which makes quantifiers non-greedy by default. And this is precisely why you should **not** use that modifier; the confusion it inevitably causes outweighs any benefit. If you want a quantifier to be non-greedy, add a `?`.
Alan Moore
@Alan, ah, I thought it had to do with Unicode, but I guess that's a lower case `u`. Thanks.
Bart Kiers