Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verbose regular expressions in PHP?

Tags:

regex

php

verbose

Searching on php.net I was not able to find any support for verbose regular expressions in php. Is this my fault for not knowing how to search for it, or is it php's fault for not implementing it?

If this feature is lacking from php, is there any other way to comment regular expressions other than breaking them up into smaller segments?

like image 503
DudeOnRock Avatar asked Oct 21 '25 13:10

DudeOnRock


1 Answers

You can also set 'expanded' mode modifier within the regex as long as its the first char past the
delimiter.

= '/(?x)
                     # A comment
     (               # (1 start), some capture
        . 
     )               # (1 end)
  /';

And/Or, it should also be available within //x context

'/
                   # A comment
   (               # (1 start), some capture
        . 
   )               # (1 end)
/x';

Or, you can freely move in/out of x-mode within the code

'/((?x)            # (1 start), some capture
        .
       (?-x: A )   # ' A ' 
   )               # (1 end)
/'

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!