Verbose regular expressions in PHP?

假如想象 提交于 2019-12-02 00:19:36

问题


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:


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)
/'


来源:https://stackoverflow.com/questions/22155048/verbose-regular-expressions-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!