如何从Javadoc注释中删除{和}之间的@link标记及其内容?

时间:2019-05-31 13:23:37

标签: python regex javadoc

我正在尝试分析Javadoc注释。我不需要@link标记的内容。我该如何删除? 常规格式为{@link.........}

使用下面的代码,我得到一个错误:ValueError: unexpected '{' in field name

with open("comments.txt", 'r') as f:
        for line in f:
           remove_link_tag = re.sub('{@link(.*?)}', lambda m: " 
              {@link{}}>".format(                                                    
                m.group(1).replace(".", "").replace(",",                                                                                                     
                  "").replace(";",                                                                                                                 "")), line, flags=re.DOTALL)

输入:

/**
    * Adds the specified source and target vertices to the graph, if not already included, and
    * creates a new edge and adds it to the specified graph similarly to the
    * {@link Graph#addEdge(Object, Object)} method.
    * {@code sourceVertex}
    * @param graph the graph for which the specified edge to be added
    * @param sourceVertex source vertex of the edge
    * @param targetVertex target vertex of the edge
    * @param <V> the graph vertex type
    * @param <E> the graph edge type
    *
    * @return The newly created edge if added to the graph, otherwise <code>
    * null</code>.
    */

输出

/**
    * Adds the specified source and target vertices to the graph, if not already included, and
    * creates a new edge and adds it to the specified graph similarly to the
    * method.
    *
    * {@code sourceVertex}
    * @param graph the graph for which the specified edge to be added
    * @param sourceVertex source vertex of the edge
    * @param targetVertex target vertex of the edge
    * @param <V> the graph vertex type
    * @param <E> the graph edge type
    *
    * @return The newly created edge if added to the graph, otherwise <code>
    * null</code>.
    */

1 个答案:

答案 0 :(得分:1)

很简单:

import re

input = """/**
    * Adds the specified source and target vertices to the graph, if not already included, and
    * creates a new edge and adds it to the specified graph similarly to the
    * {@link Graph#addEdge(Object, Object)} method.
    *
    * @param graph the graph for which the specified edge to be added
    * @param sourceVertex source vertex of the edge
    * @param targetVertex target vertex of the edge
    * @param <V> the graph vertex type
    * @param <E> the graph edge type
    *
    * @return The newly created edge if added to the graph, otherwise <code>
    * null</code>.
    */"""

out = re.sub('{@link.*?}', '', input)

输出:

/**
     * Adds the specified source and target vertices to the graph, if not already included, and
     * creates a new edge and adds it to the specified graph similarly to the
     *  method.
     *
     * @param graph the graph for which the specified edge to be added
     * @param sourceVertex source vertex of the edge
     * @param targetVertex target vertex of the edge
     * @param <V> the graph vertex type
     * @param <E> the graph edge type
     *
     * @return The newly created edge if added to the graph, otherwise <code>
     * null</code>.
     */