英文:
Find words in double brackets and add a character
问题
我想匹配双括号之间的任何单词,并添加额外字符(使用Notepad++或EmEditor)。例如以下文本:
[[test]], 看看我的 [[test|testing]] 它是如何测试的; 我有一个 [[test]], 你知道一个 [[test,还有更多测试]] 和 [[还有另一个测试,你知道]]。
应该变成:
[[test한]], 看看我的 [[test한|testing한]] 它是如何测试的; 我有一个 [[test한]], 你知道一个 [[test한,还有更多한 test한]] 和 [[还有另一个한 test한,你知道한]].
英文:
I want to match any words contained between double brackets and add an extra character (using Notepad++ or EmEditor). For example the following text:
[[test]], look at my [[test|testing]] how it tests; I have a [[test]],you know a [[test, and more test]] and [[there's another test you know]]
Should become
[[test한]], look at my [[test한|testing한]] how it tests; I have a [[test한]],you know a [[test한, and한 more한 test한]] and [[there한's한 another한 test한 you한 know한]]
So far I can only match the full content: \[\[.*?\]\]
答案1
得分: 2
严格的正则表达式版本如下:
(?:\G(?!\A)|\[\[)(?:(?!\[\[|]]).)*?\K\w+(?=.*?]])
这个正则表达式在[[
和]]
之间找到一个或多个单词字符。请参见正则表达式演示。
较不严格的模式如下:
(?:\G(?!\A)|\[\[)(?:(?!\[\[|]]).)*?\K\w+
请注意末尾缺少前瞻。这个正则表达式在[[
和]]
之间或仅在[[
和字符串末尾之间找到一个或多个单词字符。请参见正则表达式演示。
如果您的文本只包含良好平衡的括号,您可以使用以下正则表达式匹配在任何零个或多个不是括号的字符之前的任何单词,后跟]]
:
\w+(?=[^][]*]])
请参见此正则表达式演示。
替换部分在所有三种情况下都是$0한
,其中$0
代表整个匹配值。
模式详细信息
(?:\G(?!\A)|\[\[)
- 要么前一次匹配的结束((?!\A)
排除了字符串的开始位置\G
),要么[[
。(?:(?!\[\[|]]).)*?
- 任何一个字符(如果.
匹配换行符 未开启,则不包括换行符,否则包括换行符),零次或多次,但尽量少次,不是[[
或]]
字符序列的起始点(因此,匹配仅在[[
和]]
之间进行,但这本身并不要求]]
就在那里)。\K
- 匹配重置操作符,从整体匹配值中丢弃到目前为止匹配的文本。\w+
- 一个或多个单词字符。(?=.*?]])
- 一个正向前瞻,需要零个或多个不包括换行符的字符(如果.
匹配换行符 选项未开启,则包括换行符),尽量少的字符,然后是]]
。
英文:
A strict regex version will be
(?:\G(?!\A)|\[\[)(?:(?!\[\[|]]).)*?\K\w+(?=.*?]])
This regex finds any one or more word characters only inside [[
and ]]
. See the regex demo.
A less stricter pattern will be
(?:\G(?!\A)|\[\[)(?:(?!\[\[|]]).)*?\K\w+
Note the missing lookahead at the end. This regex finds any one or more word characters only inside [[
and ]]
or just between [[
and end of the string.
See this regex demo.
If your text contains only well balanced brackets, you may go for a regex that will match any word before any zero or more chars other than brackets followed with ]]
:
\w+(?=[^][]*]])
See this regex demo.
The replacement will be $0한
in all three cases where $0
represents the whole match value.
Pattern details
(?:\G(?!\A)|\[\[)
- either the end of the preceding match ((?!\A)
excludes start of string position from\G
) or[[
(?:(?!\[\[|]]).)*?
- any one char (other than line break chars if.
matches newline is OFF, else including newlines), zero or more but as few as possible occurrences, that is not the starting point for[[
or]]
char sequences (thus, matching is done only between[[
and]]
but this alone does not require]]
to be right there)\K
- a match reset operator that discards the text matched so far from the overall match value\w+
- one or more word chars(?=.*?]])
- a positive lookahead that requires zero or more chars other than line break chars (if.
matches newline option is OFF, else including newlines) as few as possible and then]]
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论