【问题标题】:Extract data above the matched python regex在匹配的 python 正则表达式之上提取数据
【发布时间】:2019-12-31 09:30:50
【问题描述】:

我有以下多行字符串:

/*dummy comment */

/* comment about sum function jkhkdhfljkldjf
  kjsdkjflskj
*/

int sum(int a,int b);

/*comment about mul function */ 

int mul(int a,int b);

如果我使用以下正则表达式,我会得到两个输出匹配:

regex -> (?P<desc>(\/\*[\s\S]+?\*\/$))(?P<fun>\s*int\s*\b\w+\b\s*\(\w+\s+.+\s*(?:;$))

第 1 场比赛:

desc:

/*dummy comment */ 

/* commect about sum function  jkhkdhfljkldjf
  kjsdkjflskj*/

fun:

int sum(int a,int b);

第 2 场比赛:

desc:

/* comment about mul function */

fun:

 int mul(int a,int b);

对于第 1 场比赛,我得到了两个 cmets,但我只想要最后一个评论,即 /* 关于 sum 函数 jkhkdhfljkldjf kjsdkjflskj*/ 我不想匹配 /* 虚拟评论 */

请帮我得到以下输出

第 1 场比赛:

desc:

/* commect about sum function jkhkdhfljkldjf
  kjsdkjflskj*/

fun:

int sum(int a,int b);

第 2 场比赛:

desc:

/* comment about mul function */

fun:

 int mul(int a,int b);

【问题讨论】:

  • 我尝试编辑帖子,但没有成功。你能格式化你的代码吗?

标签: python regex


【解决方案1】:

我无法调试您的正则表达式,因为它在示例中的格式似乎不正确,所以这里有一个关于如何做的工作 sn-p。仔细查看 cmets,因为它们解释了正则表达式的工作原理。

import re

# sample text as in the question
sample_str = """/*dummy comment */

/* comment about sum function */

int sum(int a,int b);

/*comment about mul function */ 

int mul(int a,int b);"""

# Match the regex below and capture its match into a backreference named “desc” (also backreference number 1) «(?P<desc>/\*\s*comment about (?P<func_name>[^\s]+?) function \*/\s*\r*\n*)»
#    Match the character “/” literally «/»
#    Match the character “*” literally «\*»
#    Match a single character that is a “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line) «\s*»
#       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
#    Match the character string “comment about ” literally (case sensitive) «comment about »
#    Match the regex below and capture its match into a backreference named “func_name” (also backreference number 2) «(?P<func_name>[^\s]+?)»
#       Match a single character that is NOT a “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line) «[^\s]+?»
#          Between one and unlimited times, as few times as possible, expanding as needed (lazy) «+?»
#    Match the character string “ function ” literally (case sensitive) « function »
#    Match the character “*” literally «\*»
#    Match the character “/” literally «/»
#    Match a single character that is a “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line) «\s*»
#       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
#    Match the carriage return character «\r*»
#       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
#    Match the line feed character «\n*»
#       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
# Match the regex below and capture its match into a backreference named “fun” (also backreference number 3) «(?P<fun>(?P<return_type>[^\s]+?) (?P<func_name_2>[^\s]+?)\((?P<arguments>[^\)]+?)\))»
#    Match the regex below and capture its match into a backreference named “return_type” (also backreference number 4) «(?P<return_type>[^\s]+?)»
#       Match a single character that is NOT a “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line) «[^\s]+?»
#          Between one and unlimited times, as few times as possible, expanding as needed (lazy) «+?»
#    Match the character “ ” literally « »
#    Match the regex below and capture its match into a backreference named “func_name_2” (also backreference number 5) «(?P<func_name_2>[^\s]+?)»
#       Match a single character that is NOT a “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line) «[^\s]+?»
#          Between one and unlimited times, as few times as possible, expanding as needed (lazy) «+?»
#    Match the opening parenthesis character «\(»
#    Match the regex below and capture its match into a backreference named “arguments” (also backreference number 6) «(?P<arguments>[^\)]+?)»
#       Match any character that is NOT the closing parenthesis character «[^\)]+?»
#          Between one and unlimited times, as few times as possible, expanding as needed (lazy) «+?»
#    Match the closing parenthesis character «\)»

function_re = re.compile(r"(?P<desc>/\*\s*comment about (?P<func_name>[^\s]+?) function \*/)\s*\r*\n*(?P<fun>(?P<return_type>[^\s]+?) (?P<func_name_2>[^\s]+?)\((?P<arguments>[^\)]+?)\))")

for function_match in function_re.finditer(sample_str):
    # match start: function_match.start()
    # match end (exclusive): function_match.end()
    # matched text: function_match.group()
    print("\ndesc:\n\n{}\n".format(function_match.group("desc")))
    print("fun:\n\n{}\n\n--------".format(function_match.group("fun")))
    # Additional groups if you need them
    # print("Func Name 1: {}".format(function_match.group("func_name")))
    # print("Func Name 2: {}".format(function_match.group("func_name_2")))
    # print("Arguments  : {}".format(function_match.group("arguments")))

这是我得到的输出:

desc:

/* comment about sum function */

fun:

int sum(int a,int b)

--------

desc:

/*comment about mul function */

fun:

int mul(int a,int b)

--------

【讨论】:

  • 嗨 Lorenzo Persichetti 感谢重播,但评论可以是 /* jkhdfhs dfhkjs*/ 之类的任何内容,也可以是多行的,我们是否可以在函数声明上方获得唯一的评论如果注释可以是单行或多行但它应该以 /* 开头并以 */ 结束
猜你喜欢
  • 1970-01-01
  • 2011-01-12
  • 1970-01-01
  • 1970-01-01
  • 2016-02-02
  • 1970-01-01
  • 2015-03-08
相关资源
最近更新 更多