arcee123
arcee123

Reputation: 211

How do I capture square brackets in regular expressions

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]+\\])

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

Thanks.

Upvotes: 1

Views: 87

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626748

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).

enter image description here

Upvotes: 2

Related Questions