0

I am regular expression newbie. I have a boundary problem to capture a content from a line of string in Python3.

Let's say I have the line of string here:

line = "(string1),DC (string2),IC\n"

What I want is list = ["(string1)", "(string2)"]


I tried to use

list = regex.findall("(\(.*\))", line)

But it returned list = ["(string1),DC (string2)"]

boated_tw
  • 404
  • 1
  • 7
  • 17

1 Answers1

1

You need to skip '(' and ')' and choose the word between them like this:

>>> import re
>>> 
>>> line = "(string1),DC (string2),IC\n"
>>> re.findall("\(\w+\)", line)
['(string1)', '(string2)']
ettanany
  • 17,505
  • 6
  • 41
  • 60