Как удалить тег @link и его содержимое между {и} из комментария Javadoc? - PullRequest
0 голосов
/ 31 мая 2019

Я пытаюсь проанализировать комментарии 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>.
    */

OUTPUT

/**
    * 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 Ответ

1 голос
/ 31 мая 2019

Sipmly as:

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>.
     */
...