AskMe
AskMe

Reputation: 2561

Regular expression to remove string between brackets

  1. My string is like this : Name (CLOSED)
  2. My string is like this : Name (CLOSED 0FF)
  3. My string is like this : Name (CLOSED OF)
  4. My string is like this : Name (DEC'D)

I want to remove both brackets and string inside the brackets, to get only : Name

I'm using this URL http://regexr.com/ and I have written regx like this: ([()]CLOSED)

However, the last bracket is not getting selected. What am I doing wrong?

Upvotes: 2

Views: 2160

Answers (5)

Ryszard Czech
Ryszard Czech

Reputation: 18611

Remove parens at the end of string:

\s*\([^()]*\)$

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  \(                       '('
--------------------------------------------------------------------------------
  [^()]*                   any character except: '(', ')' (0 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  \)                       ')'
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

Upvotes: 1

SamWhan
SamWhan

Reputation: 8332

Don't know why Estebans answer doesn't work for you, but at least this should:

Replace

\s*\([^)]*\)

with nothing (empty string).

See it here at regex101 (it appears regexr doesn't save the substitution string so I used regex101 instead).

Upvotes: 1

CIYRUS
CIYRUS

Reputation: 11

Try This ([(]CLOSED[)]) You forgot to add the closing brackets

Upvotes: 0

slash
slash

Reputation: 603

You can try this one
([()]CLOSED.)

Upvotes: 0

Esteban
Esteban

Reputation: 1815

You can use this regex : ([\s]*[(][^)]*[)])

This will match any space character following by ( following by any character which is not ) and )

If what you want is specifically the CLOSED text, just use : ([\s]*[(]CLOSED[)])

See http://regexr.com/3g2sl

Upvotes: 0

Related Questions