Ben
Ben

Reputation: 62484

Regex - optional value

preg_match_all('/<p class="row">(.+?)<\/span>(.+?)- <a href="(.+?)">(.+?)<\/a>(.+?)<font size="-1"> \((.+?)\)<\/font>(.+?)<\/p>/is', $HTML, $matches);

I have the following regex, the problem is that only sometimes does the section actually show up. The regex I have requires the font to be there, how can I make it optional?

Not only mkae it optional, but pull a value if it exists

Upvotes: 0

Views: 1573

Answers (2)

Aaron Fi
Aaron Fi

Reputation: 10426

This is working for me:

(?:<font size="-1">(.+?)<\/font>)?

Further example:

% perl -e '$x = "ab<font size=\"-1\">foo</font>"; print "$1 $2" if $x =~ /(ab)(?:<font size="-1">(.+?)<\/font>)?/'
ab foo

% perl -e '$x = "ab<font size=\"-1\">foo</fontXXXXXXX>"; print "$1 $2" if $x =~ /(ab)(?:<font size="-1">(.+?)<\/font>)?/'
ab 

Upvotes: 1

Dave
Dave

Reputation: 14198

You can make font optional like:

(<\/font>)?

Upvotes: 0

Related Questions