views:

158

answers:

6

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

And is there any place I can learn regular expressions? I know the basics.

+10  A: 

This seems to be used to match the comments /* … */:

  • /\* matches the leading /*
  • [^*]*\*+ matches any following characters except * followed by one or more *
  • ([^/][^*]*\*+)* matches zero or more sequences of characters beginning with any character except / (to avoid a premature end since the last character is a *), followed by any character except *, followed by one or more *
  • / matches the ending /
Gumbo
A walking RegExp parser. +1
Marko
WoW Excellent.. Why didnt I get that lol.. You always anwer my questions :) thanks again.. But I guess it removes all empty spaces and lines as well? does it ?
atif089
no, it doesn't because it must start with a /
narcisradu
Yikes, +1, I've been staring at regexes for years (and built some pretty horrific ones myself), but I wasn't even brave enough to try figuring that one out.
Nicholas Knight
@atif089: Yes, complementary character classes describe any other character except the listed. So `[^*]` for example describes any other character except `*`.
Gumbo
+1 I nearly got it, but wasn't too sure of the subpattern in there.
BoltClock
@atif It matches `/* asd ***/*/`
Amarghosh
I use The Regex Coach (Windows only) http://www.weitz.de/regex-coach/ to work through my regex patterns. Its very helpful to step over matches etc.
Treffynnon
A: 

Precise the context where is used the regexp, it might be helpful.

Guillaume Lebourgeois
this is not an answer, ask your questions in the comments
unbeli
I can't comment other posts than mine....
Guillaume Lebourgeois
+1  A: 

It does what in says in the comment above that line. It removes comments (from CSS files).

You likely found it at

Gordon
yes exactly. Got it on phpsnippets
atif089
+13  A: 
!             # ... 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 ...
                                         .——————<—————————————<————————————.
                .———<——.       .——<——.   |          .——<———.       .——<——. |
                |      |       |     |   |          |      |       |     | |
[ / ]—>—[ * ]—>—o—[^*]—' .—>—[ * ]—>—o—>—o—>—[^/]—>—o—[^*]—' .—>—[ * ]—>—o—' .—>—[ / ]
                |        |               |          |        |               |
                '————>———'               |          '———>————'               |
                                         '——————>——————————————>—————————————'

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

Example instance:

/* blah *** f /* foo*** */
12333333444566675666777578

This is used to remove C-style comments.

KennyTM
`O_O` Awesome visualization! And I like "and finally a flash". ;P
deceze
@deceze: Oops slash :p
KennyTM
i like the ascii art :)
atamanroman
yeah love the diagram, they are FSMs after all
the0ther
A: 

It means somebody was drunk.

Seriously, I don't even want to try decoding that completely, but it looks like it was designed to replace some combination of forward slashes and asterisks with nothing (e.g. remove them).

That's the kind of regex that gives regexes a bad name.

In addition to Chris's suggestion of regular-expressions.info, you should actually review the reference docs for PHP's regexes, which are actually PCREs (Perl Compatible Regular Expressions), meaning you should probably also read through the Perl regex docs.

It's been ages since I picked up an edition of it, but I seem to recall that Mastering Regular Expressions from O'Reilly was a good book on regexes in general:

Nicholas Knight
A: 

Taking things one step at a time:

The ! are just used as a delimiter for the start/end of the regex, so they aren't used for matching.

/\* matches a forward slash followed by a star (the star is a special character so is escaped by a backslash).

[^*]* matches 0 or more characters that aren't stars.

\*+ matches one or more stars.


[^/] matches anything but a forward slash

[^*]* matches 0 or more characters that aren't stars.

\*+ matches 1 or more stars.


The last bit in brackets, followed by a star, matches that section 0 or more times.

/ matches another forward slash.

Overall, that matches any pattern like /*asdf***asdfasdf***/, ie it is matching that style of comment.