Extra characters being returned from regexp

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
mzfp2
Forum Contributor
Posts: 137
Joined: Mon Nov 11, 2002 9:44 am
Location: UK
Contact:

Extra characters being returned from regexp

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
mzfp2
Forum Contributor
Posts: 137
Joined: Mon Nov 11, 2002 9:44 am
Location: UK
Contact:

Post by mzfp2 »

Thanks Feyd, I see the solution based on your explanation.
Post Reply