这个正则表达式意味着什么

Ati*_*din 1 php regex preg-replace

$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);

我有没有可以学习正则表达式的地方?我知道基础知识.

ken*_*ytm 13

!             # ... Beginning of the regex...
    /         # 1. Match a slash
    \*        # 2.  and an asterisk
    [^*]*     # 3.   followed by some non-asterisks
    \*+       # 4.    and then at least 1 asterisks
    (         #    And then some groups of
     [^/]     # 5.  match a non-slash
     [^*]*    # 6.   followed by some non-asterisks
     \*+      # 7.    and then at least 1 asterisks
    )*        #
    /         # 8. And finally a slash
!             # ... End of the regex ...
Run Code Online (Sandbox Code Playgroud)
                                         .——————<—————————————<————————————.
                .———<——.       .——<——.   |          .——<———.       .——<——. |
                |      |       |     |   |          |      |       |     | |
[ / ]—>—[ * ]—>—o—[^*]—' .—>—[ * ]—>—o—>—o—>—[^/]—>—o—[^*]—' .—>—[ * ]—>—o—' .—>—[ / ]
                |        |               |          |        |               |
                '————>———'               |          '———>————'               |
                                         '——————>——————————————>—————————————'

  1       2        3*          4+        (    5         6*         7+      )*      8 

示例实例:

/* blah *** f /* foo*** */
12333333444566675666777578
Run Code Online (Sandbox Code Playgroud)

这用于删除C风格的注释.


Gum*_*mbo 10

这似乎用于匹配评论/* … */:

  • /\* 匹配领先 /*
  • [^*]*\*+匹配以下任何字符,但*后跟一个或多个字符*
  • ([^/][^*]*\*+)*匹配零个或多个以任何字符开头的字符序列(除了/避免因为最后一个字符为a而过早结束*),后跟除了之后的任何字符*,后跟一个或多个*
  • / 匹配结尾 /

  • 一个行走的RegExp解析器.+1 (3认同)