用于在特定位置添加字符的正则表达式

2024-05-15 23:39:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用python分析Javadoc注释,为此,我需要使用句号进行拆分。如何在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)

这将在所需的位置添加一个句号,除了在最后一个标记之后,这对拆分不是问题!你知道吗


Tags: andthereturnifhereparamismethod
1条回答
网友
1楼 · 发布于 2024-05-15 23:39:41

对于那些缺少.的表达式,您只需编写一个表达式,可能类似于:

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

你可以用$1$2.代替它

enter image description here

正则表达式

可以在regex101.com中修改/更改表达式。你知道吗

正则表达式电路

您还可以在jex.im中可视化您的表达式:

enter image description here

JavaScript演示

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

描述表达式

如果您想在描述之后添加.,那么this expression可能会起作用:

([\s\*]+@param)

Python代码

# 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.

相关问题 更多 >