0

I have a string that contains multiple occurrences of text enclosed in square brackets that I need to remove such as:

10/21/2012 12:12:15 [12:12:28] Admitted Last,First (Card #555) at Lobby Turnstile # 4 (IN) [In] [Noticed]

I tried String.replaceAll, replaceFirst using the regex "\[.*\]" which removes all the texts between the first [ and last ] and I end up with

10/21/2012 12:12:15

I'm stuck on how to specify the expression. Any help would be appreciated.

glez
  • 1,034
  • 3
  • 12
  • 34
  • 1
    Try "\[.*?\]" - the extra "?" means the match is non-greedy so should stop at each "]" (untested, hence comment rather than answer) – BunjiquoBianco May 16 '13 at 16:17

1 Answers1

8

use a non-greedy quantifier: "\[.*?\]"

or specifically exclude the close char: "\[[^]]*\]"

Vivin Paliath
  • 87,975
  • 37
  • 202
  • 284
jtahlborn
  • 50,774
  • 5
  • 71
  • 112
  • Perfect. The non-greedy qualifier worked...Thanks. rec = rec.replaceAll("\\[.*?\\]", ""); – glez May 16 '13 at 17:23