PHP RegEx won't pick up question mark

2019-07-22 19:49发布

问题:

I am trying to match urls in a string using the PHP function "preg_match_all". It works fine, except it will not match urls with question marks in them.

For example, this will match fine:

http://espn.com/mlb

But this will not match:

http://espn.com/mlb?player=71

Here is the regex I am using,

$regexUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

I cannot figure out why the question mark is not being picked up by the \S. I've tried a lot of different expressions and cannot get the question mark to match. Any ideas?

EDIT:

It turns out preg_match_all was returning true, but I was not escaping the question mark in the preg_match_all output, so the preg_replace call that I was making later on was failing.

回答1:

The question mark means that the preceding match is optional, i.e.

/https?/

will cause both "http" and "https" to match. You must escape the question mark to match it.

For example:

/https\?/

will now only match "https?".