e_hoog
e_hoog

Reputation: 37

How do I get the contents of the second or third set of brackets with only Regular Expression?

If I have a string like this:

Report - No Adj - Direct Deposit (1) (64402117-acdd-44f9-a9de-53a5b83b961a) (2014-08-20).dotx

I can get the contents of the first set of brackets using:

\(([^)]*)\)

I'm using a third party program which only lets me pass in a RegEx string.

What Regular Expression would give me the contents of the second and third sets of brackets?

Upvotes: 0

Views: 143

Answers (2)

Amal
Amal

Reputation: 76666

To match only the contents of the third set of brackets, you can use the following regex:

(?:\([^()]+\).*?){2}\(([^()]+)\)

Explanation:

(?:         # Begin group
  \(        #   Match '('
  [^()]+    #   Match any character that is not a '(' or ')'
  \)        #   Match ')'
  .*?       #   Content outside the brackets (until the next '(')
){2}        # Repeat the group exactly 2 times
\(          # Match '('
(           # Begin first capturing group
  [^()]+    #   Match any character that is not a '(' or ')'
)           # End first capturing group
\)          # Match ')'

RegEx Demo

Upvotes: 1

Dalorzo
Dalorzo

Reputation: 20024

What you need to is make your RegEx expression global g like:

http://regex101.com/r/rR8rR5/1

Upvotes: 0

Related Questions