Page 1 of 1

Extra characters being returned from regexp

Posted: Tue Apr 10, 2007 5:29 pm
by mzfp2
I have the following regullar expression

preg_match_all('# \(?\d+ lenses\)?\s-\s\#GBP\#\d{1,3}.\d{1,2}#i', $this->CurrentPage, $arr_Temp);

The expression matches for the following :

90 Lenses - £27.00
or
(90 Lenses) - £27.00

The expression almost works fine, it returns an array as follows :

[0] => 90 lenses - #GBP#27.00
[1] => (180 lenses) - #GBP#54.00

The only problem is, it returns a '(' and ')' after lenses which I do not want it to do, and I have spent a while now trying to get it to return the word lenses only when the info '90 lenses' is between '(' and ')' as element [1] indicates in the array dump above.

I have highlighted the portion of the expression by embolding it above which I beleive is the problem, it is looking for one or none '(' or ')', when it finds one it returns it unfortunately.

Is there a way to modify the expression so it does not return the brackets when '90 Lenses' is between brackets?

Any help would be hugely appreciated!

Posted: Tue Apr 10, 2007 7:15 pm
by feyd
The zero element array will contain the parens if they were there. There's nothing you can do about that. What you can do instead is add some additional parens (not escaped) to create two groupings of the lens count and the price, respectively, then combine them afterward.

Posted: Thu Apr 12, 2007 12:55 pm
by mzfp2
Thanks Feyd, I see the solution based on your explanation.