为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 python 中提取正式的 java 方法参数并将它们添加到字符串列表中?

如何在 python 中提取正式的 java 方法参数并将它们添加到字符串列表中?

回首忆惘然 2022-12-15 10:59:10
我有一个 java 方法,我需要从方法声明中传递给这个方法的参数。下面的代码给出了应该提取方法声明但输出None我希望将提取的参数添加到字符串列表中。import ref = open("source_code.txt", "r")source_code = f.read()match = re.match("(public|protected|private|static|\s) +[\w\<\>\[\]]+\s+(\w+) *\([^\)]*\) *(\{?|[^;])", source_code)print(match)我想要这样的东西:输入:/**    * Adds a subset of the edges of the specified source graph to the specified destination graph.    * The behavior of this operation is undefined if either of the graphs is modified while the    * operation is in progress. {@link #addEdgeWithVertices} is used for the transfer, so source    * vertexes will be added automatically to the target graph.    *    * @param destination the graph to which edges are to be added    * @param source the graph used as a source for edges to add    * @param edges the edges to be added    * @param <V> the graph vertex type    * @param <E> the graph edge type    *    * @return <tt>true</tt> if this graph changed as a result of the call    */public static <V, E> boolean addAllEdges(Graph<? super V, ? super E> destination, Graph<V, E> source, Collection<? extends E> edges)   {       boolean modified = false;       for (E e : edges) {           V s = source.getEdgeSource(e);           V t = source.getEdgeTarget(e);           destination.addVertex(s);           destination.addVertex(t);           modified |= destination.addEdge(s, t, e);       }       return modified;   }这从方法声明中提取destination, source, :edgepublic static <V, E> boolean addAllEdges(Graph<? super V, ? super E> destination, Graph<V, E> source, Collection<? extends E> edges)输出:extracted_arguments = ['destination', 'source', 'edge']
查看完整描述

1 回答

?
慕娘9325324

TA贡献1783条经验 获得超5个赞

此外,您必须为此使用解析器

在这里找到方法 args 和 split

import re

match = re.search("(public|protected|private|static) .*\((.*)\)", source_code)

args=[i.split()[-1] for i in re.sub('<.*?>','',match[2]).split(',')]

print(args)


查看完整回答
反对 回复 2022-12-15
  • 1 回答
  • 0 关注
  • 139 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号