PHP清理正则表达式 [英] php clean up regex

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

问题描述

嘿,我可以从中清理php中的preg_match:

hey can i clean up a preg_match in php from this:

preg_match_all("/(".$this->reg['wat'].")?(".$this->reg['wat'].")?(".$this->reg['wat'].")?(".$this->reg['wat'].")?(".$this->reg['wat'].")?(".$this->reg['wat'].")?(".$this->reg['wat'].")?/",$value,$match);

看起来像这样:

preg_match_all("/
   (".$this->reg['wat'].")?
   (".$this->reg['wat'].")?
   (".$this->reg['wat'].")?
   (".$this->reg['wat'].")?
   (".$this->reg['wat'].")?
   (".$this->reg['wat'].")?
   (".$this->reg['wat'].")?
/",$value,$match);

现在每个空格都算作一次中断,因此搜索时不会返回任何发现.但它看起来更简洁易读,这就是为什么我问你知道的原因.我正在寻找正则表达式中以"/"结尾的字母之一.谢谢

right now each space, it counts as a ling break so it wont return any finds when searching. but it just looks cleaner and easier to read is why i ask you know. i was looking for one of those letters to add after the closing "/" in the regex. thanks

推荐答案

是的,看看 x(PCRE_EXTENDED)
如果设置了此修饰符,则在模式中的空白数据字符将被完全忽略,除非已转义或在字符类内部,并且字符类外部的未转义#与下一个换行符(包括)之间的字符是也忽略了.这等效于Perl的/x修饰符,并可以在复杂的模式中包含注释.但是请注意,这仅适用于数据字符.空格字符可能永远不会出现在模式中的特殊字符序列中,例如,在序列中(?(会引入条件子模式.

x (PCRE_EXTENDED)
If this modifier is set, whitespace data characters in the pattern are totally ignored except when escaped or inside a character class, and characters between an unescaped # outside a character class and the next newline character, inclusive, are also ignored. This is equivalent to Perl's /x modifier, and makes it possible to include comments inside complicated patterns. Note, however, that this applies only to data characters. Whitespace characters may never appear within special character sequences in a pattern, for example within the sequence (?( which introduces a conditional subpattern.

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

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