Hackerss.com

Hackerss.com is a community of amazing hackers

Hackerss is a community for developers, data scientitst, ethical hackers, hardware enthusiasts or any person that want to learn / share their knowledge of any aspect of digital technology.

Create account Log in
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

Top comments (0)