在正则表达式中获取错误

时间:2016-11-01 17:28:08

标签: php regex wordpress

我正在尝试获取网站的主题名称...并在正则表达式中获取问题。如果你看到 angle-child-theme 是url。我需要获得这个价值。

>>>pairSum([7,8,5,3,4,6], 11)
0 4
1 3
2 5

目前我正在使用此正则表达式来获取值

http://example.com/wp-content/themes/angle-child-theme/style.css

问题出在网站源代码的顶部,我的代码行

$searchfor = $url."/wp-content/themes/";
// escape special characters in the query
$pattern = preg_quote($searchfor, '/');
// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*\$/U";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $file, $matches)){
     //Match found
}

然后我的正则表达式获得值角度而不是 angle-child-theme

编辑1: 这是我正在使用的正则表达式

http://example.com/wp-content/themes/angle/assets/images/favicons/favicon.ico

任何人都可以帮我解决这个正则表达式吗?

https://regex101.com/r/nR2uaj/2

由于

2 个答案:

答案 0 :(得分:0)

我已经解决了我的问题:

这是将获得我所需值的正则表达式:

/http\:\/\/example\.com\/wp\-content\/themes\/([^\/]+)\/style.css/

答案 1 :(得分:0)

如果您需要抓取and之间的网址的一部分,请使用

$re = '@http://example\.com/wp-content/themes/([^/]+)/style\.css@';

并获取$matches[1]值。请注意,@正则表达式分隔符允许将转义保持在最小值,并且必须转义所有与文字点匹配的点。 ([^/]+)部分是捕获组,匹配并捕获除/以外的1个或多个字符(因为[^...]否定字符类)。

请参阅PHP demo

$re = '@http://example\.com/wp-content/themes/([^/]+)/style\.css@';
$str = 'http://example.com/wp-content/themes/angle/assets/images/favicons/favicon.ico\nhttp://example.com/wp-content/themes/angle-child-theme/style.css';
preg_match_all($re, $str, $matches);
print_r($matches[1]);