Provide an example for the pseudo-regex: Match every u开发者_运维技巧rl except those from example.com and example2.com according to the PHP regexp syntax.
Here is what I have so far, but it doesn't work:
$patternToMatch = "@https?://[^(example.com|example2.com)]\"*@i";
Don't use regular expressions for things you don't need to.
$parts = parse_url($url);
if ($parts && $parts['host'] != 'example.com' && $parts['host'] != 'example2.com') {
// the URL seems OK
}
The problem here is that within a class definition ([]
) special characters such as (
and |
lose their meaning.
A better solution is to match on example.com or example2.com and then proceed only for negative tests.
No, everything between square brackets will match just one character. For example the regex:
[^example]
will match any single character other than e
, x
, a
, m
, p
, l
and e
.
Try negative lookahead:
@https?://(www\.)?(?!example2?.com)@i
You almost had the answer. This will do the matching that you want.
$patternToMatch = "@https?://(example.com|example2.com)@i";
精彩评论