regex temizlemek php

3 Cevap php

hey ben bu gelen php bir preg_match temizlemek:

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

Bu gibi bakmak:

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

o ararken herhangi bir buluntu dönmek olmaz bu yüzden hemen her alanda, bir ling mola olarak sayar. i bilmeniz neden soruyorsun ama sadece temiz ve okunması kolay olduğu görünüyor. i regex kapanış "/" sonra eklemek için bu harflerin biri arıyordu. teşekkürler

3 Cevap

Evet, bakmak PCRE_EXTENDED option:

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.
preg_match_all('/('.$this->reg['wat'].'){0,7}/', $value, $match);

Bu oldukça temiz olmalıdır.

Durumda belirgin değildi: (ve VolkerK cevabı bir uzantısı / basitleştirilmiş versiyonu gibi) preg_match çağrısına x bayrağı eklemek:

preg_match_all(".../x",$value,$match);

Bu aslında onları eşleşen regex olmadan satırsonlarını kullanmanızı sağlayacak.