问题描述
PHP手册在 http://php.net/manual/zh/reference.pcre.pattern.modifiers.php
因此,它的用法与应该使用几次的模式有关,这些模式内部没有锚点(例如^
,$
)或固定的起始字符序列,例如以类似'/^abc/'
的模式.
但是没有具体的细节,例如应用此修饰符及其实际作用方式.
它仅适用于当前正在执行的脚本的PHP线程,并且在脚本执行后,模式的缓存"分析丢失了吗?还是引擎将模式分析存储在全局缓存中,然后供使用PCRE且标记有此修饰符的模式的多个PHP线程使用?
此外,从PCRE引言中: http://php.net/manual/en/intro.pcre.php
如果仅对每个线程使用"S"修饰符,它与已编译的正则表达式的PCRE缓存有何不同?我猜想存储了其他信息,就像MySQL在索引表中的行时所做的事情一样(当然,对于PCRE,此附加信息存储在内存中).
最后但并非最不重要的是,有人使用过该修饰符的真实用例吗?您是否注意到改进之处并欣赏其好处?
感谢您的关注.
PHP文档引用了PCRE文档的一小部分.这是来自 PCRE 8.36 的更多详细信息(重点为我):
...
请注意,在更高版本的PCRE(v10.00,也称为PCRE2)中,该库已进行了大规模的重构和API重新设计.结果之一是,研究总是在PCRE 10.00及更高版本中进行的.我不知道PHP何时会使用PCRE2,但是迟早会发生,因为PCRE 8.x从现在开始将不会获得任何新功能.
以下是 PCRE2发布公告的引文:
关于第二个问题:
PCRE本身没有缓存,但是PHP维护了正则表达式的缓存,以避免一遍又一遍地重新编译相同的模式,例如,在循环中使用preg_
函数的情况.
the PHP manual states the following about the PCRE's "S" (Extra analysis of pattern) modifier on http://php.net/manual/en/reference.pcre.pattern.modifiers.php
So its usage is related to patterns which should be used several times, without anchors inside of them (such as ^
, $
) or a fixed starting char sequence, e.g. in a pattern like '/^abc/'
.
But there aren't any specific details on where e.g. apply this modifier and how it actually works.
Does it apply only for the PHP thread of the current executing script and after the script is executed the "cached" analysis of the pattern is lost? Or does the engine store the analysis of the pattern in a global cache which is then made available to several PHP threads that use PCRE with the pattern marked with this modifier?
Also, from the PCRE introduction: http://php.net/manual/en/intro.pcre.php
If the "S" modifier is used per-thread only, how does it differs from the PCRE cache of compiled regexps? I guess additional information is stored, something like MySQL does when it indexes the rows in a table (of course in the case of PCRE, this additional information is stored in memory).
Last, but not the least, have someone experienced a real use case where he/she had used this modifier and did you notice an improvement and appreciate its benefits?
Thanks for the attention.
PHP docs quote a small part of the PCRE docs. Here are some more details (emphasis mine) from PCRE 8.36:
...
Please note that in the later PCRE version (v10.00, also called PCRE2), the lib has undergone a massive refactoring and API redesign. One of the consequences is that studying is always performed in PCRE 10.00 and above. I don't know when PHP will make use of PCRE2, but it will happen sooner or later because PCRE 8.x won't get any new features from now on.
Here's a quote from the PCRE2 release announcment:
As for your second question:
There's no cache in PCRE itself, but PHP maintains a cache of regexes to avoid recompiling the same pattern over and over again, for instance in case you use a preg_
function inside a loop.
这篇关于PCRE PHP"S"的用法和实用性的具体示例. (模式的额外分析)修饰符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!