用于匹配单行和多行注释的Python正则表达式。(Python regex for matching single line and multi line comments.)

编程入门 行业动态 更新时间:2024-10-28 16:22:52
用于匹配单行和多行注释的Python正则表达式。(Python regex for matching single line and multi line comments.)

我正在尝试为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 comment

and

/* 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 */']

更多推荐

本文发布于:2023-08-07 14:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464746.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注释   正则表达式   regex   matching   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!