Text Extraction
Text Between Square Brackets Regex
Square brackets show up in log prefixes like [ERROR], in ticket tags such as [Bug], in wiki markup and in template placeholders. Because a bracket is a special character in regular expressions, both the opening and closing brackets are escaped with a backslash. The middle part accepts any character except another square bracket, which keeps each match to the innermost pair and prevents one match from swallowing two separate tags. Group 1 returns the label without brackets, which is exactly what you need when you want to count log levels, collect tags from a title or list the placeholders used in a template.
Pattern (JavaScript)
/\[([^\[\]]*)\]/Matches the character "[", then a group (any character except \[\], zero or more times), then the character "]".
Examples
Matches
- ✓ [ERROR] disk full
- ✓ Fix login [Bug] [UI]
- ✓ Hello [name]
Does not match
- ✗ no brackets
- ✗ (parentheses only)
- ✗ [open but never closed
Try it
4 matches (JavaScript engine)
[ERROR] disk full Fix login [Bug] [UI] Hello [name] no brackets (parentheses only) [open but never closed
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /\[([^\[\]]*)\]/g;
const text = "[ERROR] disk full";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'\[([^\[\]]*)\]')
text = '[ERROR] disk full'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/\[([^\[\]]*)\]/';
$text = '[ERROR] disk full';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("\\[([^\\[\\]]*)\\]");
String text = "[ERROR] disk full";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\[([^\[\]]*)\]`)
text := "[ERROR] disk full"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"\[([^\[\]]*)\]");
var text = "[ERROR] disk full";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"\[([^\[\]]*)\]").unwrap();
let text = "[ERROR] disk full";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /\[([^\[\]]*)\]/
text = '[ERROR] disk full'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why are the brackets escaped with backslashes?
- An unescaped [ starts a character class in regex syntax. Writing \[ and \] tells the engine to match literal bracket characters instead.
- What happens with nested brackets like [a [b] c]?
- The pattern only matches the innermost pair, so it returns b. Regular expressions in JavaScript cannot balance arbitrarily nested brackets; use a small parser if nesting matters.
- Will it match empty brackets?
- Yes. The star allows zero characters, so [] matches with an empty group 1. Change the star to a plus if you only want brackets that contain text.