Соответствие однострочных комментариев JavaScript (//) - PullRequest
6 голосов
/ 26 января 2010

Я бы хотел отфильтровать (в основном однострочные) комментарии из (в основном, допустимого) JavaScript с помощью модуля re python. Например:

// this is a comment
var x = 2 // and this is a comment too
var url = "http://www.google.com/" // and "this" too
url += 'but // this is not a comment' // however this one is
url += 'this "is not a comment' + " and ' neither is this " // only this

Я сейчас пытаюсь это больше получаса без какого-либо успеха. Может кто-нибудь, пожалуйста, помогите мне?

РЕДАКТИРОВАТЬ 1 :

foo = 'http://stackoverflow.com/' // these // are // comments // too //

РЕДАКТИРОВАТЬ 2 :

bar = 'http://no.comments.com/'

Ответы [ 2 ]

7 голосов
/ 26 января 2010

Мои способности к регулярным выражениям устарели, поэтому я использовал ваш вопрос, чтобы обновить то, что я помню. Это стало довольно большим регулярным выражением в основном потому, что я также хотел фильтровать многострочные комментарии.

import re

reexpr = r"""
    (                           # Capture code
        "(?:\\.|[^"\\])*"       # String literal
        |
        '(?:\\.|[^'\\])*'       # String literal
        |
        (?:[^/\n"']|/[^/*\n"'])+ # Any code besides newlines or string literals
        |
        \n                      # Newline
    )|
    (/\*  (?:[^*]|\*[^/])*   \*/)        # Multi-line comment
    |
    (?://(.*)$)                 # Comment
    $"""
rx = re.compile(reexpr, re.VERBOSE + re.MULTILINE)

Это регулярное выражение соответствует трем различным подгруппам. Один для кода и два для содержания комментариев. Ниже приведен пример того, как извлечь их.

code = r"""// this is a comment
var x = 2 * 4 // and this is a comment too
var url = "http://www.google.com/" // and "this" too
url += 'but // this is not a comment' // however this one is
url += 'this "is not a comment' + " and ' neither is this " // only this

bar = 'http://no.comments.com/' // these // are // comments
bar = 'text // string \' no // more //\\' // comments
bar = 'http://no.comments.com/'
bar = /var/ // comment

/* comment 1 */
bar = open() /* comment 2 */
bar = open() /* comment 2b */// another comment
bar = open( /* comment 3 */ file) // another comment 
"""

parts = rx.findall(code)
print '*' * 80, '\nCode:\n\n', '\n'.join([x[0] for x in parts if x[0].strip()])
print '*' * 80, '\nMulti line comments:\n\n', '\n'.join([x[1] for x in parts if x[1].strip()])
print '*' * 80, '\nOne line comments:\n\n', '\n'.join([x[2] for x in parts if x[2].strip()])
1 голос
/ 26 января 2010

Может быть легче разобрать, если у вас были явные точки с запятой.

В любом случае это работает:

import re

rx = re.compile(r'.*(//(.*))$')

lines = ["// this is a comment", 
    "var x = 2 // and this is a comment too",
    """var url = "http://www.google.com/" // and "this" too""",
    """url += 'but // this is not a comment' // however this one is""",
    """url += 'this "is not a comment' + " and ' neither is this " // only this""",]

for line in lines: 
    print rx.match(line).groups()

Вывод вышеуказанного:

('// this is a comment', ' this is a comment')
('// and this is a comment too', ' and this is a comment too')
('// and "this" too', ' and "this" too')
('// however this one is', ' however this one is')
('// only this', ' only this')

Я не уверен, что вы делаете с javascript после удаления комментариев, но JSMin может помочь. В любом случае, он удаляет комментарии достаточно хорошо, и в python есть реализация .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...