Using sublime text regex find and replace, how can I capture the first occurrence of a closing bracket ]?
使用sublime text regex查找和替换,如何捕获第一次出现的右括号]?
This regex works as expected and captures up to the first opening bracket:
这个正则表达式按预期工作,并捕获到第一个开放括号:
^([^\[]*)\[
你_ 你_ [
ni3] you (informal, as opposed to courteous 您[nin2])_
你_你_ [ni3]你(非正式的,而不是礼貌的你[nin2])_
But this, for closing bracket, doesn't pick up anything:
但是,对于结束括号,这不会取得任何东西:
^([^\]]*)\]
1 个解决方案
#1
2
According to your screenshot, the Whole Word option is enabled:
根据您的屏幕截图,启用了整个Word选项:
Thus, the ]
you match requires a word char after it and the first char should be a word char, too.
因此,你匹配需要一个单词char后面,第一个char也应该是单词char。
A possible solution for you use just disabling the option.
您可能使用的解决方案是禁用该选项。
However, if you just need to select/highlight/replace/remove the first ]
in the file, you may use
但是,如果您只需要在文件中选择/突出显示/替换/删除第一个],则可以使用
\A[^]]*\K]
where:
-
\A
- matches the start of the document -
[^]]*
- matches 0+ chars other than]
-
\K
- a match reset operator that omits the whole text matched so far -
]
- matches a literal]
.
\ A - 匹配文档的开头
[^]] * - 匹配除0以外的0个字符
\ K - 匹配重置运算符,省略了到目前为止匹配的整个文本
] - 匹配文字]。
You do not need to escape ]
outside a character class and when it is the first char inside a character class in PCRE/Boost regex flavor.
您不需要在字符类之外转义,而且它是PCRE / Boost正则表达式中字符类中的第一个字符。
Note that if you need to match the first ]
on each row, you need to replace \A
with the ^
(start of a line anchor) and add \r\n
to the negated character class:
请注意,如果您需要匹配每行的第一个],则需要将\ A替换为^(行锚的开头)并将\ r \ n添加到否定的字符类:
^[^]\r\n]*\K]
#1
2
According to your screenshot, the Whole Word option is enabled:
根据您的屏幕截图,启用了整个Word选项:
Thus, the ]
you match requires a word char after it and the first char should be a word char, too.
因此,你匹配需要一个单词char后面,第一个char也应该是单词char。
A possible solution for you use just disabling the option.
您可能使用的解决方案是禁用该选项。
However, if you just need to select/highlight/replace/remove the first ]
in the file, you may use
但是,如果您只需要在文件中选择/突出显示/替换/删除第一个],则可以使用
\A[^]]*\K]
where:
-
\A
- matches the start of the document -
[^]]*
- matches 0+ chars other than]
-
\K
- a match reset operator that omits the whole text matched so far -
]
- matches a literal]
.
\ A - 匹配文档的开头
[^]] * - 匹配除0以外的0个字符
\ K - 匹配重置运算符,省略了到目前为止匹配的整个文本
] - 匹配文字]。
You do not need to escape ]
outside a character class and when it is the first char inside a character class in PCRE/Boost regex flavor.
您不需要在字符类之外转义,而且它是PCRE / Boost正则表达式中字符类中的第一个字符。
Note that if you need to match the first ]
on each row, you need to replace \A
with the ^
(start of a line anchor) and add \r\n
to the negated character class:
请注意,如果您需要匹配每行的第一个],则需要将\ A替换为^(行锚的开头)并将\ r \ n添加到否定的字符类:
^[^]\r\n]*\K]