-1

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

to look like 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);

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

David
  • 2,365
  • 8
  • 34
  • 59

3 Answers3

6

Yes, take a look at the 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.
VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • well this is inside a class so is that why its not working? i added it in and it doesn't find any results. – David Apr 25 '10 at 23:59
  • This answer has been added to the [Stack Overflow Regular Expression FAQ](http://stackoverflow.com/a/22944075/2736496), under "Modifiers". – aliteralmind Apr 10 '14 at 00:52
3
preg_match_all('/('.$this->reg['wat'].'){0,7}/', $value, $match);

That should be fairly clean.

erisco
  • 14,154
  • 2
  • 40
  • 45
1

In case it wasn't obvious: (and as an extension/simplified version of VolkerK's answer) add the x flag to the preg_match call:

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

This will allow you to use newlines without regex actually matching them.

Henrik
  • 982
  • 1
  • 8
  • 10