import re
s = re.findall(r'[^()]+', 'Loaf of Fresh White Bread (1 lb)')[0].rstrip()
to apply this to whole list use the following code. (given_list->result_list)
import re
given_list = ['Milk (regular) (1 gallon)', 'Loaf of Fresh White Bread (1 lb)', 'Rice (white) (1 lb)', 'Eggs (regular) (12)', 'Local Cheese (1 lb)']
result_list = [re.findall(r'[^()]+', x)[0].rstrip() for x in given_list]
print(result_list)
# prints ['Milk', 'Loaf of Fresh White Bread', 'Rice', 'Eggs', 'Local Cheese']
Using regex is very tricky.
I recommend you to take a look at
regular expression automata theory to be familar with this tool.
Explanation of the code:
r'[^()]+' can be dissected into []+ and ^()
'[]' is a set of tokens(letters).
we define some set of tokens within [].
'+' means iteration of at least 1 time.
'[]+' means that certain set of tokens have been iterated 1 or more times.
'^' means complement set.
In simple terms it means "set of everything except something"
"something" here is '(', and ')'.
so "everything but parentheses" set is made.
and iteration of that set of more than 1 times.
So in human language this means
"a string of any character except '(' or ')', of length 1 or more."
findall method finds all substrings that satisfy this condition,
and makes a list of it.
[0] returns the first element of it.
rstrip removes the trailing whitespace since we couldn't remove it with regex.
Since you only need the first result of this regex search, re.search can do the job faster. (it finds the first match and stops)
Example:
import re
given_list = ['Milk (regular) (1 gallon)', 'Loaf of Fresh White Bread (1 lb)', 'Rice (white) (1 lb)', 'Eggs (regular) (12)', 'Local Cheese (1 lb)']
result_list = [re.search(r'[^()]+', x).group(0).rstrip() for x in given_list]
print(result_list)
# prints ['Milk', 'Loaf of Fresh White Bread', 'Rice', 'Eggs', 'Local Cheese']