亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 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條經驗 獲得超4個贊

此外,您必須為此使用解析器

在這里找到方法 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 關注
  • 83 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號