3

I'm trying to substitute all chars inside () alongside with what's inside them but there is a problem. In the output it leaves whitespaces at start and end.

Code:

import re
regex = r"\(.+?\)"

test_str = ("(a) method in/to one's madness\n"
    "(all) by one's lonesome\n"
    "(as) tough as (old boot's)\n"
    " (at) any moment (now)   \n"
    "factors (in or into or out)        \n"
    " right-to-life\n"
    "all mouth (and no trousers/action)\n"
    "(it's a) small world\n"
    "  throw (someone) a bone ")

subst = ""

result = re.sub(regex, subst, test_str, 0, re.MULTILINE)

if result:
    print (result)

Result:

    method in/to one's madness
 by one's lonesome
 tough as 
  any moment    
factors         
 right-to-life
all mouth 
 small world
  throw  a bone 

I tried different patterns (other than this) to remove the \s from start but then when it finds the space at the end of any line it combines the following lines to the preceding one's.

Mujtaba
  • 251
  • 1
  • 13

2 Answers2

1

You can use

regex = r"[^\S\r\n]*\([^()]*\)"
result = "\n".join([x.strip() for x in re.sub(regex, "", test_str).splitlines()])

See the Python demo

The [^\S\r\n]*\([^()]*\) regex will remove all instances of

  • [^\S\r\n]* - zero or more horizontal whitespaces and then
  • \([^()]*\) - (, any zero or more chars other than ( and ) and then )

The "\n".join([x.strip() for x in re.sub(regex, "", test_str).splitlines()]) part splits all text into lines, strips them from leading/trailing whitespace and joins them back with a line feed.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
0

You can go with /\(([^)]+)\)/g So basically:

  • \(: matches the opening parenthesis
  • ([^)]+: matches a group of characters
  • \): matches the closing parenthesis
  • /g: all matches

User you re.sub(...) to replace all the regex matches.

Sam
  • 121
  • 7