如何在正则表达式中捕获方括号

时间:2020-12-13 21:44:28

I have a series of text elements that contains brackets:

我有一系列包含括号的文本元素:

[PropertyID]
,[ParcelID]
,[Score]
,[Score1]

How do I capture the elements WITH the brackets? I tried this and it didn't work: ([\\[a-zA-Z0-9]+\\]]) also (\\[[a-zA-Z0-9]+\\])

如何使用括号捕获元素?我试过这个并没有用:( [\\ [a-zA-Z0-9] + \\]])也是(\\ [[a-zA-Z0-9] + \\])

I'm trying to perform some text replacement in notepad++ and It says It can't find it.

我正在尝试在记事本++中执行一些文本替换,它说它无法找到它。

Thanks.

1 个解决方案

#1


2  

You can use

您可以使用

\[[^][]*]

The \[ matches a [, then [^][]* matches zero or more characters other than [ and ] (since it is a negated character class with a * quantifier applied to it) and ] matches a literal ].

\ [匹配[,然后[^] [] *匹配[和]以外的零个或多个字符(因为它是一个带有*量词的否定字符类)和]匹配文字]。

This will match [...] substrings with no ] and [ inside them.

这将匹配[]]没有]和[在他们内部]的子串。

The first [ must be escaped to match a literal [ symbol. When you use [\\[a-zA-Z0-9]+\\]], the first [ starts a character class that matches 1 symbol, either \, or [, a-z, A-Z, 0-9, one or more times (+), and then a literal \]] sequence (see what your regex actually matches).

第一个[必须转义以匹配文字[符号]。当你使用[\\ [a-zA-Z0-9] + \\]]时,第一个[启动一个匹配1个符号的字符类,要么是\,要么[,az,AZ,0-9,一个或多个时间(+),然后是文字\]]序列(看看你的正则表达式实际上匹配)。

如何在正则表达式中捕获方括号

#1


2  

You can use

您可以使用

\[[^][]*]

The \[ matches a [, then [^][]* matches zero or more characters other than [ and ] (since it is a negated character class with a * quantifier applied to it) and ] matches a literal ].

\ [匹配[,然后[^] [] *匹配[和]以外的零个或多个字符(因为它是一个带有*量词的否定字符类)和]匹配文字]。

This will match [...] substrings with no ] and [ inside them.

这将匹配[]]没有]和[在他们内部]的子串。

The first [ must be escaped to match a literal [ symbol. When you use [\\[a-zA-Z0-9]+\\]], the first [ starts a character class that matches 1 symbol, either \, or [, a-z, A-Z, 0-9, one or more times (+), and then a literal \]] sequence (see what your regex actually matches).

第一个[必须转义以匹配文字[符号]。当你使用[\\ [a-zA-Z0-9] + \\]]时,第一个[启动一个匹配1个符号的字符类,要么是\,要么[,az,AZ,0-9,一个或多个时间(+),然后是文字\]]序列(看看你的正则表达式实际上匹配)。

如何在正则表达式中捕获方括号