tags:

views:

37

answers:

1

I am using php preg_match and I have the original expression as:

|(label\">Carried Price    </span> £)((.)*?)( </div)|

But I want to be able to able to be able to use the same basic expression but be able to search for Carried Price or Web Exclusive. I tried something like:

|(label\">[Carried Price|Web Exclusive]    </span> £)((.)*?)( </div)|

But no luck, any ideas? I also have to keep this structure of the regex as 3 groupings.

Additional the code I am using to test with example text

$page = '  <div class="web-exclusive-price">     <span class="label">Web Exclusive 
</span> £399.00 </div>
   <div class="web-only-label">     <span class="label">Offer not available in-store    </span>  </div>
    <div class="deliveryShortcut">        <span class="label"><a href="#deliverySection" onclick="navigateOpenSection(\'delivery\');">Standard Delivery</a></span> Free    </div>
';

$r = '|(label\">(?:Carried Price|Web Exclusive)    </span> £)((.)*?)( </div)|';

preg_match($r, $page, $matches);
+3  A: 

You need round braces for this. Square braces [] are used to specify set of characters. Here is fixed regexp:

$r = "'(label\">(?:Carried Price|Web Exclusive)\s+</span> £)((.)*?)( </div)'";

Symbol (?:...) means non-capturing group. Here is more information about groups http://www.regular-expressions.info/named.html

Update 1:

Also regexp is not the best way to parse html. Look at allternavite way using SimpleXML.

Update 2 (thanks to Tim Pietzcker):

If you're using | as regexp delimeter then you can't use it inside regexp. I've replaces it with ' symbol. \s+ is used to match any number of spaces. Now your test code is working. See example above.

Ivan Nevostruev
Thanks for that but I've inherited a system that use this implementation, i'll definitely consider that way for next time! I cant seem to get the code to work as I get the error " Unknown modifier 'W' " Any ideas?
Joe
You probably need to drop the `|` characters at the beginning and end of the regex. Also, there seems to be a discrepancy between whitespace in the regex and the example. I'd suggest to use `\s+` after `...Exclusive)` instead of spaces. This will also match newlines as seem to be present.
Tim Pietzcker
@Tim Pietzcker: Thanks! That's the other reason. I've replaced RE delimeter this '
Ivan Nevostruev
@tim thank you so much for the \s+ tip and the new delimiter!!! I really appreciated, its got me stomped for the past day basically!
Joe