簡體   English   中英

RegEx用於在特定位置添加字符

[英]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*
     */

注意:如果句號/分號/逗號已經存在,則不需要替換,因為我的程序基於這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)

這將在所需的位置添加一個句號,除了最后一個標記之后,這不是拆分的問題!

對於那些失蹤的人. ,您只需編寫一個類似於以下內容的表達式:

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

您可以將其替換為$1$2.

在此處輸入圖片說明

正則表達式

您可以在regex101.com中修改/更改表達式。

RegEx電路

您還可以在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 = `$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代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM