RegEx для добавления символа в определенных местах - PullRequest
0 голосов
/ 15 мая 2019

Я пытаюсь проанализировать комментарии Javadoc в python, и для этого мне нужны полные остановки для расщепления.Как добавить полные остановки в нужных местах в комментарии Javadoc?

Я хочу что-то вроде этого: Ввод:

/**
     * The addVertex method checks to see if the vertex isn't null, and then if
     * the graph does not contain the vertex, the vertex is then added and true
     * is returned
     *
     * @param vertex
     *
     * @throws NullPointerException.
     *
     * @return b
     */

Вывод:

/**
     * The addVertex method checks to see if the vertex isn't null, and then if
     * the graph does not contain the vertex, the vertex is then added and true
     * is returned.*here*
     *
     * @param vertex.*here*
     *
     * @throws NullPointerException.*here*
     *
     * @return b.*here*
     */

Примечание. Если точка / точка с запятой / запятая уже существует, замена выполняетсяне требуется, так как моя программа разделяется на основе этих 3 знаков препинания.

Кроме того, описания Javadoc могут иметь встроенные теги, такие как {@link ...}, знаки препинания не требуются.

Только до того, как потребуется @param, @throw, @return (также в конце).

РЕШЕНИЕ

test_str = ("/**\n"
    "     * The addVertex method checks to see if the vertex isn't null, and then if\n"
    "     * the graph does not contain the vertex, the vertex is then added and true\n"
    "     * is returned\n"
    "     *\n"
    "     * @param vertex\n"
    "     *\n"
    "     * @throws NullPointerException.\n"
    "     *\n"
    "     * @return b\n"
    "     */")

result = re.sub(r'(@param|@throw|@return)', r'.\1', test_str)
print(result)

Это добавляетполная остановка в необходимых местах, кроме последнего тега, не проблема для расщепления!

1 Ответ

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

Для тех, у кого отсутствует ., вы можете просто написать выражение, похожее на:

(\* @param|@return)(.*)

который вы можете заменить на $1$2.

enter image description here

RegEx

Вы можете изменять / изменять свои выражения в regex101.com .

RegEx Circuit

Вы также можете визуализировать свои выражения в jex.im :

enter image description here

Демонстрация JavaScript

const regex = /(\* @param|@return)(.*)/gm;
const str = `/**
     * The addVertex method checks to see if the vertex isn't null, and then if
     * the graph does not contain the vertex, the vertex is then added and true
     * is returned
     *
     * @param vertex
     *
     * @throws NullPointerException.
     *
     * @return b
     */`;
const subst = `$1$2.`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);

Код Python:

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"(\* @param|@return)(.*)"

test_str = ("/**\n"
    "     * The addVertex method checks to see if the vertex isn't null, and then if\n"
    "     * the graph does not contain the vertex, the vertex is then added and true\n"
    "     * is returned\n"
    "     *\n"
    "     * @param vertex\n"
    "     *\n"
    "     * @throws NullPointerException.\n"
    "     *\n"
    "     * @return b\n"
    "     */")

subst = "\\1\\2."

# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)

if result:
    print (result)

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

выход

/**
     * The addVertex method checks to see if the vertex isn't null, and then if
     * the graph does not contain the vertex, the vertex is then added and true
     * is returned
     *
     * @param vertex.
     *
     * @throws NullPointerException.
     *
     * @return b.
     */

Выражение для описания

Если вы хотите добавить . после описания, это выражение может работать:

([\s\*]+@param)

Python Code

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"([\s\*]+@param)"

test_str = ("/**\n"
    "     * The addVertex method checks to see if the vertex isn't null, and then if\n"
    "     * the graph does not contain the vertex, the vertex is then added and true\n"
    "     * is returned\n"
    "     *\n"
    "     * @param vertex\n"
    "     *\n"
    "     * @throws NullPointerException.\n"
    "     *\n"
    "     * @return b\n"
    "     */")

subst = ".\\1"

# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)

if result:
    print (result)

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
...