正则表达式 - 匹配 http 但不匹配带约束的 https
Regex - match http but not https with constraint
PHP 正则表达式
来源:
http://example.com/wp-content/uploads/2017/01/image.jpg
https://example.com/wp-content/uploads/2017/01/image2.jpg
http://example.com/wp-content/plugins/example-plugin/images/image.jpg
Objective
我想匹配所有字符串:
- 包含 HTTP,但不包含 HTTPS
- 包含
wp-content/uploads/
.. 我不想捕获 wp-content/uploads/
部分,所以据我所知这是一个非捕获组。
我试过做一个积极的前瞻,但我似乎做不好。
到目前为止,这是我想出的,但我不知道将 HTTP 部分放在哪里。 regex101 的正则表达式测试器不匹配。
(?=(?:(wp-content\/uploads)+))
更新:
澄清一下,我需要简单的正则表达式,不需要 PHP 代码。换句话说,PHP 使用的 PCRE。
如果您只是在寻找 YES/NO 那么下面的方法就可以了:
http[^s].*wp-content\/uploads\/
请参阅 https://regex101.com/r/kPxOMt/1 示例。
如果您想捕获 url 的一部分,请告诉我,我会更新正则表达式。
类似的东西:
<?php
$strings = [
'http://example.com/wp-content/uploads/2017/01/image.jpg',
'https://example.com/wp-content/uploads/2017/01/image2.jpg',
'http://example.com/wp-content/plugins/example-plugin/images/image.jpg'
];
$pattern = '/(http[^s]).+(wp-content\/uploads/)(.+)/';
foreach ($strings as $subject) {
if (preg_match($pattern, $subject, $matches)) {
echo $matches[3] . "\n";
}
}
I do not want to capture the wp-content/uploads/
$res = '';
if (preg_match('~(http://.*?)wp-content/uploads/(\S*)~', $string, $m) {
$res = $m[1] . $m[2];
}
PHP 正则表达式
来源:
http://example.com/wp-content/uploads/2017/01/image.jpg
https://example.com/wp-content/uploads/2017/01/image2.jpg
http://example.com/wp-content/plugins/example-plugin/images/image.jpg
Objective
我想匹配所有字符串:
- 包含 HTTP,但不包含 HTTPS
- 包含
wp-content/uploads/
.. 我不想捕获 wp-content/uploads/
部分,所以据我所知这是一个非捕获组。
我试过做一个积极的前瞻,但我似乎做不好。 到目前为止,这是我想出的,但我不知道将 HTTP 部分放在哪里。 regex101 的正则表达式测试器不匹配。
(?=(?:(wp-content\/uploads)+))
更新:
澄清一下,我需要简单的正则表达式,不需要 PHP 代码。换句话说,PHP 使用的 PCRE。
如果您只是在寻找 YES/NO 那么下面的方法就可以了:
http[^s].*wp-content\/uploads\/
请参阅 https://regex101.com/r/kPxOMt/1 示例。
如果您想捕获 url 的一部分,请告诉我,我会更新正则表达式。
类似的东西:
<?php
$strings = [
'http://example.com/wp-content/uploads/2017/01/image.jpg',
'https://example.com/wp-content/uploads/2017/01/image2.jpg',
'http://example.com/wp-content/plugins/example-plugin/images/image.jpg'
];
$pattern = '/(http[^s]).+(wp-content\/uploads/)(.+)/';
foreach ($strings as $subject) {
if (preg_match($pattern, $subject, $matches)) {
echo $matches[3] . "\n";
}
}
I do not want to capture the wp-content/uploads/
$res = '';
if (preg_match('~(http://.*?)wp-content/uploads/(\S*)~', $string, $m) {
$res = $m[1] . $m[2];
}