Hackerss.com

hackerss
hackerss

Posted on

Regex to select all that is inside of parenthesis including the parenthesis in Python

Regex to select all that is inside of parenthesis including the parenthesis - Python:


import re

def regex_select_parenthesis(string):
    """
    Regex to select all that is inside of parenthesis including the parenthesis.

    Parameters
    ----------
    string : str
        String to select from.

    Returns
    -------
    list
        List of all that is inside of parenthesis including the parenthesis.
    """

    #return the list of all that is inside of parenthesis including the parenthesis
    return re.findall(r'\([^()]*\)', string)

#example
result = regex_select_parenthesis('(a) (b) (c) (d)')
#output
print(result)  #['(a)', '(b)', '(c)', '(d)']


Enter fullscreen mode Exit fullscreen mode

Oldest comments (0)