Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

291
Vistas
Java Regular expression matching square brackets

I'm trying to do the following using regular expression (java replaceAll):

**Input:**
Test[Test1][Test2]Test3

**Output**
TestTest3

In short, i need to remove everything inside square brackets including square brackets.

I'm trying this, but it doesn't work:

\\[(.*?)\\]

Would you be able to help?

Thanks,
Sash

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can try this regex:

\[[^\[]*\]

and replace by empty

Demo

Sample Java Source:

final String regex = "\\[[^\\[]*\\]";
final String string = "Test[Test1][Test2]Test3\n";
final String subst = "";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
final String result = matcher.replaceAll(subst);
System.out.println(result);
about 4 years ago · Santiago Trujillo Denunciar

0

Your original pattern works for me:

String input = "Test[Test1][Test2]Test3";
input = input.replaceAll("\\[.*?\\]", "");
System.out.println(input);

Output:

TestTest3

Note that you don't need the parentheses inside the brackets. You would use that if you planned to capture the contents in between each pair of brackets, which in your case you don't need. It isn't wrong to have them in there, just not necessary.

Demo here:

Rextester

about 4 years ago · Santiago Trujillo Denunciar

0

\[\w+]

This works for me, this regex matches all the words which are enclosed in square brackets.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda