G
G
Gasoid2015-12-15 13:49:05
Python
Gasoid, 2015-12-15 13:49:05

What is a way to remove unclosed parentheses from a string along with the content if there are no closed blocks after them?

For example:
from 'dfd((df)(adf'
get 'dfd((df)'

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
Gasoid, 2015-12-15
@Gasoid

import re
import sys


def main():
    text = sys.argv[1]
    pattern = re.compile(r'\([^\)]+$')
    result = pattern.sub('', text)
    if result:
        print result


if __name__ == '__main__':
    main()

so

D
DDDsa, 2015-12-15
@DDDsa

www.regexr.com

O
Oleg Tsilyurik, 2015-12-15
@Olej

What nonsense! ;-)
Why is the 1st '(' closed? ... then you get: 'dfd'.
In general, the bracket notation is parsed by pushing onto the stack by '(' and popping the level by ')'. See check bracket expression

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question