我正在尝试为PLY创建一个python正则表达式,它将匹配表单的注释
// some comment和
/* comment more comment */所以我试过了
t_COMMENT = r'//.+ | /\*.+\*/'但这不允许多行注释,当我尝试使用'dot matches all'选项解决这个问题时
t_COMMENT = r'//.+ | (?s) /\*.+\*/'它导致'//'注释类型匹配许多行。 此外,如果我尝试有两个单独的正则表达式,如
t_COMMENT = r'//.+' t_COMMENT2 = r'(?s) /\*.+\*/''//'注释类型仍匹配多行,就好像点匹配所有选项一样。
有谁知道如何解决这个问题?
I'm trying to create a python regex, for PLY, which will match comments of the form
// some commentand
/* comment more comment */So I tried
t_COMMENT = r'//.+ | /\*.+\*/'but this doesn't allow for multi line comments and when I try to solve this using the 'dot matches all' options like
t_COMMENT = r'//.+ | (?s) /\*.+\*/'it results in the '//' comment type matching many lines. Also if I try to have two separate regexes like
t_COMMENT = r'//.+' t_COMMENT2 = r'(?s) /\*.+\*/'the '//' comment type still matches multiple lines as though the dot matches all option is selected.
Does anybody know how to solve this?
最满意答案
以下正则表达式将匹配两种类型的注释,
(?://[^\n]*|/\*(?:(?!\*/).)*\*/)DEMO
>>> s = """// some comment ... ... foo ... bar ... foobar ... /* comment ... more comment */ bar""" >>> m = re.findall(r'(?://[^\n]*|/\*(?:(?!\*/).)*\*/)', s, re.DOTALL) >>> m ['// some comment', '/* comment\n more comment */']The below regex would match both type of comments,
(?://[^\n]*|/\*(?:(?!\*/).)*\*/)DEMO
>>> s = """// some comment ... ... foo ... bar ... foobar ... /* comment ... more comment */ bar""" >>> m = re.findall(r'(?://[^\n]*|/\*(?:(?!\*/).)*\*/)', s, re.DOTALL) >>> m ['// some comment', '/* comment\n more comment */']更多推荐
发布评论