开发者

Why does the regex pattern not match the code?

开发者 https://www.devze.com 2023-04-12 20:30 出处:网络
Do you have any idea why this regex does not match the code below? $regex = \"!<title>(.*?)</title>!i\";

Do you have any idea why this regex does not match the code below?

$regex = "!<title>(.*?)</title>!i";

code:

<b:include data='blog' name='all-head-content'/>
<title>
    <b:if cond='data:blog.homepageUrl == data:blog.url'>
        <data:blog.title/>
    <b:else/>
        <data:blog.pageName/> &raquo; <data:blog.title/>
    </b:if>
</title>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesh开发者_Python百科eet' type='text/css'/>


Don't use regular expressions to parse XML.

Your expression doesn't work because the dot doesn't match new lines. Adding the DOTALL modifier /.../s would fix that, but then you'll have other problems like matching greedily instead of lazy.

Using an XML parser would be a better idea.


Here is my suggestion for a correct regex:

(?<=<title>)(.*)(?=</title>)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号