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

RegEx for adding a char in specific places

我正在尝试分析 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*
     */

注意:如果完整的 stop/semicolon/comma 已经存在,则不需要替换,因为我的程序是根据这 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'.', test_str)
print(result)

除了最后一个标签之后,在需要的地方添加了一个句号,拆分不是问题!

对于那些缺少 . 的人,您可以简单地写一个类似于:

的表达式
(\* @param|@return)(.*)

你可以用.

替换它

正则表达式

您可以 modify/change 您的表达式 regex101.com

正则表达式电路

您还可以在 jex.im:

中可视化您的表情

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 = `.`;

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

描述表达式

如果您想在描述后添加 .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.