0

I need to read in lines from a text file, basically everything after and including the first open parenthesis can be removed with an empty string. I came up with a regex:

>>> line = "Bar Harbor (College of the Atlantic)"
>>> re.sub(r"(.*) ?\(.*$","\1", line)
'\x01'

The following works, but excludes lines that do not have parentheses which is not according the specs (so it does not work)

match = re.match("(.*?) ?\(.*$",line)
if match:
    towns.append(match.group(1))

What am i doing wrong in te first substitute? Regexs are not necessary, I just need to remove everything after the first parentheses, so also if there is more than one way I'm also interested.

dr jerry
  • 9,768
  • 24
  • 79
  • 122

1 Answers1

0

Here's a way to do it without regular expressions:

In [1]: x = "Bar Harbor (College of the Atlantic)"

In [6]: idx = x.find('(')

In [7]: x[:idx] if idx > 0 else x
Out[7]: 'Bar Harbor '
ffledgling
  • 11,502
  • 8
  • 47
  • 69