tags:

views:

76

answers:

1

Dear all,

I want to write the regular expression in php for matching the line within a double and single quotes. Actually I am writing the code for removing comment lines in css file.

Like:

"/* I don't want to remove this line */"

but

/* I want to remove this line */

Eg:

- valid code /* comment */ next valid code "/* not a comment */" /* this is comment */

Expected result:

- valid code next valid code "/* not a comment */"

Please any one give me a regular expression in php for my requirement.

+1  A: 

The following should do it:

preg_replace( '/\s*(?!<\")\/\*[^\*]+\*\/(?!\")\s*/' , '' , $theString );

Test case:

$theString = '- valid code /* comment */ next valid code "/* not a comment */" /* this is comment */';

preg_replace( '/(?!<\")\/\*[^\*]+\*\/(?!\")/' , ' ' , $theString );

# Returns 'valid code next valid code "/* not a comment */" '
Lucanos