wordpress中的正则表达式假定界符 [英] regular expression false delimiter in wordpress

查看:59
本文介绍了wordpress中的正则表达式假定界符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是正则表达式的新手,我有这个简单的疑问.

I am newbie to regular expression and I have this simple doubt.

我在 wordpress 中找到了这段代码

I have found this code in wordpress

$self = preg_replace('|^.*/wp-admin/|i', '', $self); 

根据 php.net 上的文档 | 不允许作为分隔符..

according to the doc on php.net | is not permissible as delimiter..

谁能解释一下代码?

推荐答案

你试过了吗?

来自您的链接:

使用 PCRE 函数时,要求模式用分隔符包围.分隔符可以是任何非字母数字、非反斜杠、非空白字符.

When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.

所以 | 是一个完全有效的分隔符.当您阅读该页面上的评论时,他们建议不要使用元字符(如 |)作为分隔符,而它们应该在正则表达式中使用.

So | is a perfectly valid delimiter. When you read the comments on that page, they suggest to not use meta characters (like |) as delimiters, when they should be used inside the regex.

因为在你的例子中没有改变 $self = preg_replace('|^.*/wp-admin/|i', '', $self); 没有问题,它正在按预期工作.

Since there is no alternation in your example $self = preg_replace('|^.*/wp-admin/|i', '', $self); there is no problem and it is working as expected.

当您在正则表达式中有更改时(例如 preg_match("|(F|f)oo|", "Foobar")),您将收到警告未知修饰符 'f'",因为解释器认为正则表达式确实在第一次交替时结束.

When you have an alternation in the regex (e.g. preg_match("|(F|f)oo|", "Foobar")) you will get a warning "Unknown modifier 'f'", because the interpreter thinks the regex does end at the first alternation.

结论:允许,但不建议使用正则表达式元字符,如 |^+、... 作为正则表达式分隔符.

Conclusion: It's allowed, but not recommended to use regex meta-characters like |, ^, +, ... as regex delimiters.

这篇关于wordpress中的正则表达式假定界符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆