开发者

help with regex match

i want to retrieve following urls with a regex:

 HREF="http://www.getty.edu/vow/TGNFullDisplay?find=&place=&nation=&english=Y&subjectid=7009830"

 HREF="http://www.getty.edu/vow/TGNFullDisplay?find=&place=&nation=&english=Y&subjectid=7009830&ptype=PF"

the difference is the ending. the first one omits the &ptype=PF and the last one includes it.

at the moment im using this pattern:

 protected $uriPattern = '/http:\/\/www\.getty\.edu\/vow\/.*?\?find=&plac开发者_JAVA技巧e=&nation=&english=Y&subjectid=......./i';

but that works only for the first one.

i wonder how the regex pattern would look like for the preg_match_all to match both of them. thanks for help.


If there is an optional part in the strings you are matching, you can add (optional)?, in your case (&ptype=PF)?.


Try this

protected $uriPattern = '/http:\/\/www\.getty\.edu\/vow\/.*?\?find=&place=&nation=&english=Y&subjectid=.......(&ptype=PF){0,1}/i';


I was going to suggest the more succinct

"/http://www\.getty\.edu/vow/TGNFullDisplay\?find=&place=&nation=&english=Y&subjectid=.+(&ptype=PF)?/i"

The forward slashes are not special in either PHP nor RegEx, and thus do not need to be escaped, and the ID could be a different length.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜