如何在正则表达式的一对括号内的每行前加#?

问题描述 投票:0回答:2

我想更改此

@author(
line 1
line 2
line 3
)

使用RegEx进行以下操作。

@author(
#line 1
#line 2
#line 3
)  

我可以像这样^@author\([^]]+\n\)来查询块,如何在每行的前面放置#号?

regex
2个回答
0
投票

对于PCRE(PHP)正则表达式引擎,可以使用正则表达式

^([^)@].*)

Demo

正则表达式正在执行以下操作:

^      # match beginning of line
(      # begin capture group 1
[^)@]  # match a character other than ')' and ']'
.*     # match 0+ chars to the end of the line
)      # end capture group 1

0
投票

假设您使用的是基于PCRE的正则表达式环境,则可以搜索

(@author\(\n|\G(?!\A).*+\n(?!\)))

并将匹配项替换为

$1#

。请参见this expression at regex101.com的说明。

© www.soinside.com 2019 - 2024. All rights reserved.