我正在尝试用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)
这将在所需的位置添加一个句号,除了在最后一个标记之后,这对拆分不是问题!你知道吗
对于那些缺少
.
的表达式,您只需编写一个表达式,可能类似于:你可以用
$1$2.
代替它正则表达式
可以在regex101.com中修改/更改表达式。你知道吗
正则表达式电路
您还可以在jex.im中可视化您的表达式:
JavaScript演示
Python代码:
输出
描述表达式
如果您想在描述之后添加
.
,那么this expression可能会起作用:Python代码
相关问题 更多 >
编程相关推荐