Caco
Caco

Reputation: 1654

Regex to capture string inside parenthesis

I have tried

[^\(]\(.*\)

but in a string like

Tamara(PER) Jorquiera

The pattern returns

a(PER)

How can I get only the text inside parenthesis, assuming the open and close parenthesis occur once?

Upvotes: 0

Views: 49

Answers (2)

karthik manchala
karthik manchala

Reputation: 13640

[^\(]\(.*\)

It matches any character that is not ( (a in the example).. and a literal ( then everything till )

You can use the following:

\([^)]*\)          // this will match only the text between parentheses
                   // including parantheses

If you want only the text use lookahead and lookbehinds:

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

See DEMO

Upvotes: 1

Kasravnd
Kasravnd

Reputation: 107287

Use following regex :

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

Demo

Note that you don't need [^\(] out side of the parenthesis just use [^)]*? within a capture group.

[^)]*? is a non-greedy regex that match anything except closing parenthesis.

Upvotes: 0

Related Questions