0

I have a large array of strings which I need to edit according to some rules. One of these rules is that a string should not have blocks with parenthesis. For example-

"(Speaking) (Laughing) Get out of here" # Desired output - "Get out of here"

or

"(Dancing)Where have you been" # Desired output - "Where have you been"

Please help me to do this.

2 Answers 2

1

A possibly interesting alternative is a simple state machine. It should be pretty performant too.

c = """(Speaking) (Laughing) Get out of here"""

paren_count = 0
out = []

for char in c:
    if char == "(":
        paren_count += 1
        continue
    elif char == ")":
        paren_count -= 1
        continue
    if paren_count == 0:
        out.append(char)

print "".join(out).strip()
Sign up to request clarification or add additional context in comments.

Comments

0
(?![^()]*\))(?!\s[^a-zA-Z])([a-zA-Z ]+)

You can use this regex to do what you want.

See demo.

http://regex101.com/r/hQ1rP0/12

use it like

import re
x="(Speaking) (Laughing) Get out of here"
print re.findall(r"(?![^()]*\))([a-zA-Z ]+)",x)

4 Comments

Perhaps worth mentioning that this regex won't handle nested parenthesis correctly.
It fails when string is like "-".
@gliese581g - is a valid input?
@gliese581g which test case does it fail.it does not accept - or .-

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.