F
F
Fastto2011-10-28 11:02:14
.NET
Fastto, 2011-10-28 11:02:14

Help with regulars

There is a task - to remove all comments from the php code. I process regulars

with the regexp .NET 3.5 class .


if (ext.ToLower() == "php" )
                {
                    string content = File.ReadAllText(file, System.Text.Encoding.UTF8);

                    content = Regex.Replace(content, @"\/\/.*(\r?\n)?", @"");
                    content = Regex.Replace(content, @"\/\*(.*(\r?\n)?)*.*\*\/", @"");
                    content = Regex.Replace(content, @"\r?\n", @" ");

                    File.WriteAllBytes(dst + fileName, System.Text.Encoding.UTF8.GetBytes( content ));
                }
}

1 - works, but according to a simplified scheme, if you slip it in for example - echo "3;//3"; // 33 - removes the wrong thing
2 - can't get a clear result - either removes half of the code or touches nothing at all
3 - works

Answer the question

In order to leave comments, you need to log in

5 answer(s)
G
gaelpa, 2011-10-28
@Fastto

It is convenient to delete comments by PHP itself “php -w”, however, it also cuts spaces.
IMHO - what you need, it is enough to have a php binary.

A
Anatoly, 2011-10-28
@taliban

If I were you, I would change the regular expressions to take into account several lines, then there would be no need to insert \r\n\s and the like into them. And secondly, you will not achieve the correct work with regulars:


\\ comment
$a = '\\ not comment'; - такое Вы учитываете?

At one time I made one thing that highlighted the code, I also needed to parse comments and text there, I tried to do everything through regular expressions, but in the end I scored and rewrote everything by brute force:
(multi-line comments should be searched like this (I searched for text lines like this but also suitable for comments):
1. go through the text character by character (or look for an opening comment in the text)
2. check if it is valid, not in the text, not in another comment, not escaped, etc.
3. look for the nearest closing comment, we also check everything
4. delete text from start position to
end
position
5. go to point 1 while there are paired comments

N
Nastradamus, 2011-10-28
@Nastradamus

usually helps me:
www.gskinner.com/RegExr/

M
Magiq, 2011-10-28
@Magiq

Purely regular expressions will be very difficult, especially parsing something like get(' example.com ');

M
Magiq, 2011-10-28
@Magiq

*
<? php get(' example.com'); ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question