Reputation: 37
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
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 parenthesisimport 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])
i need an example in a bracket
Upvotes: 0
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.(
, 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