conditional regex
I've parsing a xml file that sometimes has the value <avg_cpc>some number</avg_cpc>
sometime don't.
my regex look like this:
<is_adult>(.*?)</is_adult>.*?<trademark_probability>(.*?)</trademark_probability>.*?<total_extensions_used>(.*?)</total_extensions_used> **here comes the <avg_cpc>some number</avg_cpc>** .*?</appraisal>
how can I make this regex match items that don't have c开发者_运维技巧pc value ?
I've tried (<avg_cpc>.*?</avg_cpc>)?
without luck.
Thanks !
Please use a real XML parser for PHP, instead of regular expressions. This will make everything much easier, not to mention less error-prone.
I would guess it's because you're not escaping your slashes, try this:
<is_adult>(.*?)<\/is_adult>.*?<trademark_probability>(.*?)<\/trademark_probability>.*?<total_extensions_used>(.*?)<\/total_extensions_used>(<avg_cpc>.*?<\/avg_cpc>)?.*?<\/appraisal>
I would also use [^<]+ instead of .*? if possible.
精彩评论