Elisa Nu
Elisa Nu

Reputation: 37

Regex to detect different type (length) of text in a closed bracket? Python

Is there a way to detect different information from closed brackets? Example:

Text = Hello World, this is my name (Greta). For this example (i need an example in a bracket)

In my case I would like to only detect the brackets with more than one word inside using a regex.

Output: I need an example in a bracket

Upvotes: 0

Views: 47

Answers (2)

ScottC
ScottC

Reputation: 4105

You could use:

\(\s*[A-Za-z]+\s[A-Za-z\s]+\))

Which will look for:

  • \( : open parenthesis
  • \s* : zero or more spaces
  • [A-Za-z]+ : followed by one or more letters
  • \s : followed by a space
  • [A-Za-z\s]+ : followed by one or more letters and/or spaces
  • \) : close parenthesis

Code:

import re

text='Hello World, this is my name (Greta). For this example (i need an example in a bracket)'

m = re.findall(r'(\(\s*[A-Za-z]+\s[A-Za-z\s]+\))', text)

for match in m:
    print(match[1:-1])

Output:

i need an example in a bracket

Upvotes: 0

Kaia
Kaia

Reputation: 907

\((\w+\s+\w+[^)]*)\) is probably close to what you want.

  • \( is an open-paren literal and \) is a close-paren literal
  • (...) creates a capturing group, so we can get only the inside of the parens
  • \w matches alphanumeric characters
  • + says that we are looking for 1 or more of the specified character class
  • \s matches whitespace characters.
  • [^)] is the character group of anything other than a close-paren )
  • [^)]* selects any number of non-) characters (including 0)
  • \) matches the close paren.
  • Taken together, we check for a (, then a alphanumeric word, then one or more spaces, then another alphanumeric word, then take as much as we need of things that aren't ), and then a close paren.

Note that this fails in cases like Hello (Alice and (hello) Bob), where it will capture Alice and (hello. (This is a fundamental limitation to regexes, you will have to use another method if you need to parse nested parentheses)

Upvotes: 0

Related Questions