Page 1 of 1

Submatch inside optional match

Posted: Mon Mar 12, 2007 10:33 pm
by regexManQ
need to have an optional match however the information I want is a submatch inside of the optional match. However in the cases where it should match it doesn't because it's optional, so the last submatch is empty

This is the full pattern:

Code: Select all

/<div style=\'font-size: 18px;.*\'>(.*)<\/div>.*Product Code: <b>(.*)<\/b>.*\\$(.*)<\/font>.*<img.*>.*<img.*>.*<img src=\'(.*)\'.*>.*(?:<div.*><b>Product Description<\/b><\/div>.*<div.*>(.*)<\/div>)?/isU
The part that I'm having difficulty with is the part on the end

Code: Select all

(?:<div.*><b>Product Description<\/b><\/div>.*<div.*>(.*)<\/div>)?
Some page have the "Product Description" html code but some don't. However because I have made it optional it doesn't match on the pages it should, if I take out the optional (?...)? part it matches fine the problem is trying to match pages that dont' have the Product Description section

Posted: Tue Mar 13, 2007 7:36 am
by feyd
Sometimes in order to get the matches we want the pattern needs to be expanded out so the once optional components aren't optional in one case and not there in the other.

Posted: Tue Mar 13, 2007 4:47 pm
by regexManQ
Thankyou

Are you suggesting having an OR in the pattern instead?

Posted: Tue Mar 13, 2007 11:13 pm
by feyd
That is what I was referring to.