Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python解析js中的多行注释_Python_Regex - Fatal编程技术网

使用python解析js中的多行注释

使用python解析js中的多行注释,python,regex,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)

我想使用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
重新导入
代码_m=”“”
/*这是一个评论*/
"""
code_s=“/*这是一条注释*/”
reg=re.compile(“/\*(?P.*)\*/”,re.DOTALL+re.M)
匹配\u m=注册匹配(代码\u m)
匹配项=注册匹配项(代码)
打印匹配项#给出匹配对象
打印匹配项,不提供任何匹配项
我将
匹配项作为
。但是
匹配\u s
起作用。这里缺少什么?

仅在字符串开头匹配,请改用

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


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

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

>>> reg.search(code_m)
<_sre.SRE_Match object at 0x7f293e94d648>
>>> reg.search(code_m).groups()
(' This is a comment. ',)
注册搜索(代码) >>>注册搜索(代码m).groups() (“这是一条评论。”,)
我还有一个问题。你能帮忙吗。