Submatch inside optional match

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

Moderator: General Moderators

Post Reply
regexManQ
Forum Newbie
Posts: 2
Joined: Mon Mar 12, 2007 10:21 pm

Submatch inside optional match

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

Post 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.
regexManQ
Forum Newbie
Posts: 2
Joined: Mon Mar 12, 2007 10:21 pm

Post by regexManQ »

Thankyou

Are you suggesting having an OR in the pattern instead?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That is what I was referring to.
Post Reply