#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    199
    Rep Power
    43

    Arrow Those Tricky Stars!


    Ok, I figured out the strings myself, but I don't understand why my solution doesn't work for comments...

    I am trying to detect /* whatever */

    Here is my regular expression...
    /\*[^(\*/)]*\*/

    ... I would expect that regular expression to work, but it doesn't...

    This regular expression behaves as expected, but not as desired:
    /\*[^(/)]*\*/

    Why won't the first regular expression match? What can I do to make it match?
  2. #2
  3. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    938
    That is because character classes always match a single character.
    So the class [^(\*/)] will match any character except '(', '*', '/' or ')'. As you see, the "normal" meta characters like '(' and ')' don't group characters inside a character class as they do outside a character class. Also, the '*' does not need escaping inside a character class and it just matches the character '*'.

    All of that said, you can match comments like this:

    Code:
    (?s)/\*((?!\*/).)*\*/
    Last edited by prometheuzz; November 24th, 2008 at 01:14 AM.

IMN logo majestic logo threadwatch logo seochat tools logo