Verbose regular expressions in PHP?

2020-04-11 11:20发布

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?

1条回答
叛逆
2楼-- · 2020-04-11 11:56

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)
/'
查看更多
登录 后发表回答