繁体   English   中英

在正则表达式中出现错误

[英]Getting error in regular expression

我正在尝试获取网站的主题名称,并用正则表达式表示问题。 如果您看到有angle-child-theme是url。 我需要获得那个价值。

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
}

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

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

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

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

http\:\/\/example\.com\/wp\-content\/themes\/

有人可以帮我这个正则表达式吗?

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

谢谢

我已经解决了我的问题:

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

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

如果您需要获取介于and之间的一部分URL,请使用

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

并获得$matches[1]值。 请注意, @ regex分隔符允许将转义符最小化,并且必须转义所有与文字点匹配的点。 ([^/]+)部分是一个捕获组 ,它匹配并捕获/以外的1个或多个字符(因为[^...]否定的字符类 )。

参见PHP演示

$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]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM