使用python从js文件解析多个注释 [英] Parse multiple comments from js file using python

查看:192
本文介绍了使用python从js文件解析多个注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取js文件的内容注释。我尝试使用代码

I want to get contents of a js file comments. I tried using the code

import re
code = """
/*
This is a comment.
*/

/*
This is another comment.
*/

"""
reg = re.compile("/\*(?P<contents>.*)\*/", re.DOTALL)
matches = reg.search(code)

if matches:
    print matches.group("contents")

This is a comment.
*/

/*
This is another comment.

如何单独获取评论?

推荐答案

重复不重复:

"/\*(?P<contents>.*?)\*/"

现在。* 将尽可能少消费,而不是尽可能多消费。

Now the .* will consume as little as possible instead of as much as possible.

您将要使用多个匹配 findall 而不是搜索

To get multiple matches you will want to use findall instead of search.

这篇关于使用python从js文件解析多个注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆