匹配文本未被一组特定字符包围的所有行

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

我正在尝试对文档中的某些条目进行查找替换(在 Notepad++ 中,但特定工具可能并不重要)。该文档的条目看起来像这样:

==Some header==
* {{Tselect|Select an option}}
** This is an option.
*** '''Someone:''' The option will have some kind of response.
*** '''Someone else:''' Possibly even multiple lines of response.
** This is a different option.
*** {{Tact|This one has a different type of response, surrounded by brackets}}

所以,在上面的例子中,我只想匹配

** This is an option.
** This is a different option.
,而不匹配其他行。此外,一旦我捕获了它,我想用以下内容包装文本(不是
*
字符):
{{Topt|The text}}
。我会保留相同数量的星号,并将星号后面的文本放在
{{Topt|
}}
内。例如,我会将
** This is an option.
变成
** {{Topt|This is an option.}}

我一直在正则表达式测试器中摆弄正则表达式,但它要么不匹配任何内容,要么几乎匹配所有内容。我知道它需要以

^(\*+) 
开头,以便它只匹配具有无限数量的星号后跟空格的行的开头(这样我就可以捕获
*
字符的确切数量,并且它需要以
$
结尾,以便将其限制在一行中。

我正在努力解决的是如何排除文本以三个

'
字符包围的内容开头的任何内容,以及由
{{
}}
字符包围的任何内容。

如果相关的话,这是维基代码语法,并且它是有时稍微缩进的项目符号项目。

regex notepad++
1个回答
0
投票
  • Ctrl+H
  • 查找内容:
    ^\*\* \K.+$
  • 替换为:
    {{Topt|$0}}
  • 勾选环绕
  • SELECT 正则表达式
  • 取消勾选
    . matches newline
  • 全部替换

说明:

^           # beginning of line
\*\*        # 2 asterisks followed by a space
\K          # forget all we have seen until this position
.+          # 1 or more any character but newline
$           # end of line

更换:

{{Topt|     # literally
$0          # the whole match (i.e. the text to be copied
}}          # literally

截图(之前):

截图(之后):

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