ChatGPT解决这个技术问题 Extra ChatGPT

Regular expression to extract text between square brackets

Simple regex question. I have a string on the following format:

this is a [sample] string with [some] special words. [another one]

What is the regular expression to extract the words within the square brackets, ie.

sample
some
another one

Note: In my use case, brackets cannot be nested.


c
codaddict

You can use the following regex globally:

\[(.*?)\]

Explanation:

\[ : [ is a meta char and needs to be escaped if you want to match it literally.

(.*?) : match everything in a non-greedy way and capture it.

\] : ] is a meta char and needs to be escaped if you want to match it literally.


The other answer's method, using [^]] is faster than non-greedy (?), and also works with regex flavours that don't support non-greedy. However, non-greedy looks nicer.
How to exclude [ ] from output(result)?
@MickeyTin, if you are using Java, you can group it using group(1) over just group(), so the '[]' will not go together
This matches only the first occurrence
How do you exclude the brackets from the return?
m
morepusto
(?<=\[).+?(?=\])

Will capture content without brackets

(?<=\[) - positive lookbehind for [

.*? - non greedy match for the content

(?=\]) - positive lookahead for ]

EDIT: for nested brackets the below regex should work:

(\[(?:\[??[^\[]*?\]))

@igaurav I've checked it and it works. It will not work however in environments which does not support lookbehinds like Javascript. Maybe that is yours case?
Adam, your nested brackets solution fails when there is a string with a . in it...
People who write those regexps you are god damn magicians. Thank you so much!
This should be the accepted answer since the asker specified the output without the brackets. The currently accepted answer will return [ '[sample]', '[some]', '[another one]' ] while this answer returns [ 'sample', 'some', 'another one' ].
The "positive lookbehind" feature may not be supported in all browsers.
j
jasonbar

This should work out ok:

\[([^]]+)\]

In my use case, the bracketed text may include new lines, and this regex works, while the accepted answer does not.
what does the character class [^]] mean? What does it match?
@Richard, The ^ negates the character class. It means "any character that is not a ]".
I think it doesn't work as expected, you should use \[([^\[\]]*)\] to get the content in the most inner bracket. If you look into lfjlksd [ded[ee]22] then \[([^]]+)\] will get you [ded[ee] while the proposed expression would return [ee]. testede in link
Can you please provide 'sed' and 'awk' examples to use this regex and extract text. Thanks.
T
Tim Pietzcker

Can brackets be nested?

If not: \[([^]]+)\] matches one item, including square brackets. Backreference \1 will contain the item to be match. If your regex flavor supports lookaround, use

(?<=\[)[^]]+(?=\])

This will only match the item inside brackets.


@KunalMukherjee: No, the regex can match any number of times. But some regex flavors needs to be told explicitly to apply the regex repeatedly (for example, by using the /g flag in JavaScript).
L
LJ Germain

If you do not want to include the brackets in the match, here's the regex: (?<=\[).*?(?=\])

Let's break it down

The . matches any character except for line terminators. The ?= is a positive lookahead. A positive lookahead finds a string when a certain string comes after it. The ?<= is a positive lookbehind. A positive lookbehind finds a string when a certain string precedes it. To quote this,

Look ahead positive (?=) Find expression A where expression B follows: A(?=B) Look behind positive (?<=) Find expression A where expression B precedes: (?<=B)A

The Alternative

If your regex engine does not support lookaheads and lookbehinds, then you can use the regex \[(.*?)\] to capture the innards of the brackets in a group and then you can manipulate the group as necessary.

How does this regex work?

The parentheses capture the characters in a group. The .*? gets all of the characters between the brackets (except for line terminators, unless you have the s flag enabled) in a way that is not greedy.


W
Wiktor Stribiżew

To match a substring between the first [ and last ], you may use

\[.*\]            # Including open/close brackets
\[(.*)\]          # Excluding open/close brackets (using a capturing group)
(?<=\[).*(?=\])   # Excluding open/close brackets (using lookarounds)

See a regex demo and a regex demo #2.

Use the following expressions to match strings between the closest square brackets:

Including the brackets:

\[[^][]*] - PCRE, Python re/regex, .NET, Golang, POSIX (grep, sed, bash)

\[[^\][]*] - ECMAScript (JavaScript, C++ std::regex, VBA RegExp)

\[[^\]\[]*] - Java, ICU regex

\[[^\]\[]*\] - Onigmo (Ruby, requires escaping of brackets everywhere)

Excluding the brackets:

(?<=\[)[^][]*(?=]) - PCRE, Python re/regex, .NET (C#, etc.), JGSoft Software

\[([^][]*)] - Bash, Golang - capture the contents between the square brackets with a pair of unescaped parentheses, also see below

\[([^\][]*)] - JavaScript, C++ std::regex, VBA RegExp

(?<=\[)[^\]\[]*(?=]) - Java regex, ICU (R stringr)

(?<=\[)[^\]\[]*(?=\]) - Onigmo (Ruby, requires escaping of brackets everywhere)

NOTE: * matches 0 or more characters, use + to match 1 or more to avoid empty string matches in the resulting list/array.

Whenever both lookaround support is available, the above solutions rely on them to exclude the leading/trailing open/close bracket. Otherwise, rely on capturing groups (links to most common solutions in some languages have been provided).

If you need to match nested parentheses, you may see the solutions in the Regular expression to match balanced parentheses thread and replace the round brackets with the square ones to get the necessary functionality. You should use capturing groups to access the contents with open/close bracket excluded:

\[((?:[^][]++|(?R))*)] - PHP PCRE

\[((?>[^][]+|(?)\[|(?<-o>]))*)] - .NET demo

\[(?:[^\]\[]++|(\g<0>))*\] - Onigmo (Ruby) demo


This \[((?>[^][]+|(?<o>)\[|(?<-o>]))*)] was 99.9% what I needed. By that, I mean I need everything inside the outermost brackets, but not the brackets themselves. IE, in your .Net demo link, it matches all of [text [2]], and I'd like the match to return "text [2]". However, I can get around that by just taking the match and doing a simple substring that skips the first and last characters. I am curious if it is possible to modify that regex ever so slightly to automatically omit the outermost brackets.
@B.O.B. You need to get the Group 1 value, see the C# demo online.
Thanks! I'll give that I try in my demo code that I am using (before I move it into the real project). Edit: that was exactly it! Thanks for the expert and exceptionally fast response).
E
Emma

Just in case, you might have had unbalanced brackets, you can likely design some expression with recursion similar to,

\[(([^\]\[]+)|(?R))*+\]

which of course, it would relate to the language or RegEx engine that you might be using.

RegEx Demo 1

Other than that,

\[([^\]\[\r\n]*)\]

RegEx Demo 2

or,

(?<=\[)[^\]\[\r\n]*(?=\])

RegEx Demo 3

are good options to explore.

If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.

RegEx Circuit

jex.im visualizes regular expressions:

https://i.stack.imgur.com/0Q8Qi.png

Test

const regex = /\[([^\]\[\r\n]*)\]/gm; const str = `This is a [sample] string with [some] special words. [another one] This is a [sample string with [some special words. [another one This is a [sample[sample]] string with [[some][some]] special words. [[another one]]`; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); }

Source

Regular expression to match balanced parentheses


L
LJ Germain

(?<=\[).*?(?=\]) works good as per explanation given above. Here's a Python example:

import re 
str = "Pagination.go('formPagination_bottom',2,'Page',true,'1',null,'2013')"
re.search('(?<=\[).*?(?=\])', str).group()
"'formPagination_bottom',2,'Page',true,'1',null,'2013'"

You should always use code formatting for regexes, wherever they appear. If the regex is in the text rather than a code block, you can use backticks to format them. (ref)
Also, the question was about square brackets ([]), not parentheses.
M
Michał Grzegorzewski

The @Tim Pietzcker's answer here

(?<=\[)[^]]+(?=\])

is almost the one I've been looking for. But there is one issue that some legacy browsers can fail on positive lookbehind. So I had to made my day by myself :). I manged to write this:

/([^[]+(?=]))/g

Maybe it will help someone.

console.log("this is a [sample] string with [some] special words. [another one]".match(/([^[]+(?=]))/g));


perfect answer I was looking for
Slick rick! Worked great and good use of running a code snippet to show how it works. Great answer.
N
Nezar Fadle

This code will extract the content between square brackets and parentheses

(?:(?<=\().+?(?=\))|(?<=\[).+?(?=\]))

(?: non capturing group
(?<=\().+?(?=\)) positive lookbehind and lookahead to extract the text between parentheses
| or
(?<=\[).+?(?=\]) positive lookbehind and lookahead to extract the text between square brackets

T
Tony Ladson

In R, try:

x <- 'foo[bar]baz'
str_replace(x, ".*?\\[(.*?)\\].*", "\\1")
[1] "bar"

..or gsub(pat, "\\1", x, perl=TRUE), where pat is the regular expression you provided..
This solution is excellent in the way that it "extracts" the content inside the brackets if there is one, otherwise you get the input.
ß
ßãlãjî

if you want fillter only small alphabet letter between square bracket a-z

(\[[a-z]*\])

if you want small and caps letter a-zA-Z

(\[[a-zA-Z]*\]) 

if you want small caps and number letter a-zA-Z0-9

(\[[a-zA-Z0-9]*\]) 

if you want everything between square bracket

if you want text , number and symbols

(\[.*\])

P
Peon
([[][a-z \s]+[]])

Above should work given the following explaination

characters within square brackets[] defines characte class which means pattern should match atleast one charcater mentioned within square brackets

\s specifies a space

+ means atleast one of the character mentioned previously to +.


In sensitive cases A-Z should add to pattern : ([[][a-zA-Z \s]+[]]) ; I think it's good way, while \ in regex patterns that defines in string marks ( " and ' ) and mixing up newbies by backslash handling in " or ' usages!
the only answer that worked for me for C++ regex (except im doing it with quotations instead of brackets). std::regex pattern{R"(["][a-zA-Z \s]+["])"};
c
citynorman

I needed including newlines and including the brackets

\[[\s\S]+\]


A
Andreas

If someone wants to match and select a string containing one or more dots inside square brackets like "[fu.bar]" use the following:

(?<=\[)(\w+\.\w+.*?)(?=\])

Regex Tester