使用python从js解析多行注释

时间:2012-10-30 17:31:57

标签: python regex

我想使用python在js文件中获取多行注释的内容。

我试过这段代码

import re
code_m = """
/* This is a comment. */
"""
code_s = "/* This is a comment*/"

reg = re.compile("/\*(?P<contents>.*)\*/", re.DOTALL + re.M) 
matches_m = reg.match(code_m)
matches_s = reg.match(code_s)
print matches_s # Give a match object
print matches_m # Gives None

matches_mNone。但matches_s有效。我在这里缺少什么?

2 个答案:

答案 0 :(得分:4)

match()仅匹配字符串的开头,而是使用search()

使用match()时,就像在正则表达式的开头有一个隐含的字符串锚点(\A)的开头。

作为旁注,除非您在正则表达式中使用re.M^并希望它们在行的开头和结尾匹配,否则您不需要$标记。您还应该使用按位OR(例如re.S | re.M),而不是在组合多个标志时添加。

答案 1 :(得分:2)

re.match测试字符串是否与正则表达式匹配。您可能正在寻找re.search

>>> reg.search(code_m)
<_sre.SRE_Match object at 0x7f293e94d648>
>>> reg.search(code_m).groups()
(' This is a comment. ',)