-2

I need a regular expression to select all the text between Opening parenthesis closing parenthesis.

Example:

xxxxxxx (Regular Expression) xxxxxxx( One ) xxxxxxx(One Word ) xxxxxxx ( Two Word) 
1) xxxxxxx(lowercase) 
2) xxxxxxx(UPPERCASE)

Result should be:

  1. Match 1: xxxxxxx (Regular Expression)
  2. Match 2: xxxxxxx( One )
  3. Match 3: xxxxxxx(One Word )
  4. Match 4: xxxxxxx ( Two Word)
  5. Match 5: xxxxxxx(lowercase)
  6. Match 6: xxxxxxx(UPPERCASE)

Remark : xxxxxxx is any charactor

Thank you.

2 Answers2

1

A python code that can help.

>>> import re
>>> s = """xxxxxxx (Regular Expression) xxxxxxx( One ) xxxxxxx(One Word ) xxxxxxx ( Two Word) 
... 1) xxxxxxx(lowercase) 
... 2) xxxxxxx(UPPERCASE)"""
>>> re.findall("([A-Za-z]+ {0,1}\(.*?\))", s, re.MULTILINE)
['xxxxxxx (Regular Expression)', 'xxxxxxx( One )', 'xxxxxxx(One Word )', 'xxxxxxx ( Two Word)', 'xxxxxxx(lowercase)', 'xxxxxxx(UPPERCASE)']
>>> 

"([A-Za-z]+ {0,1}\(.*?\))" is the regex being used.

  • [A-Za-z]+ means one or more occurances of any char in A-Z or a-z
  • ` {0,1} means 0 or 1 occurance of space
  • .* means a character and then zero or more occurrence of chars.
  • ? means a non-greedy way.
  • \( & /) are backslashed as we want to match them too.
shadyabhi
  • 16,675
  • 26
  • 80
  • 131
0

Try this:

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

Here is the live demo:

http://regexr.com?300k2

Alternatively if you need to explicitly access the content before parenthesis and the content inside parenthesis use the following regexp which gives names to proper groups:

(?<outpar>[^\(\)]*)\((?<inpar>[^\)]*)\) 

And again this is its live demo:

http://regexr.com?300k5

Sina Iravanian
  • 16,011
  • 4
  • 34
  • 45