简体   繁体   中英

Get all words that come after specific word

I have a list that contains strings like this:

['White Buns (Hot Dog)', ' 2 x Danish (Almond Danish) - A delight with coffee! \nThe best fruit fillings and of coarse butter filled flaky puff pastry.\n']

I want to get all the words that come before all words in brackets and the words in brackets too as to get the following:

White Buns (Hot Dog)
2 x Danish (Almond Danish)

Is there a way to accomplish this? I've tried regex but have no idea how to specify (a word in brackets)

I would use str.partition :

li=['White Buns (Hot Dog)', ' 2 x Danish (Almond Danish) - A delight with coffee! \nThe best fruit fillings and of coarse butter filled flaky puff pastry.\n']

>>> [''.join(s.partition(')')[:2]) for s in li]
['White Buns (Hot Dog)', ' 2 x Danish (Almond Danish)']

Which works regardless if a ')' is present or not.

Given:

li=['No paren in this string...', 
    'Divide me (after) <- please', 
    'More that (1) paren (2) here']

>>> [''.join(s.partition(')')[:2]) for s in li]
['No paren in this string...', 'Divide me (after)', 'More that (1)']

If you want to use a regex, use re.sub to find and keep the first part:

>>> [re.sub(r'([^)]*[)]).*', r'\1', s) for s in li]
['No paren in this string...', 'Divide me (after)', 'More that (1)']

Don't forget the .* after the ([^)]*[)]) or re.sub just reassembles your string.

To specify parenthensis you can use '\)' (equal to ')') and '\(' (equals to '(')

import re #regex lib

pattern = "(.*?)(\(.*?\))"

text =  't2 x Danish (Almond Danish) - A delight with coffee! \nThe best fruit fillings and of coarse butter filled flaky puff pastry.\n'

result = re.search(pattern, text)
print(result[0]) #all string found
print(result[1]) #first group (.*?)
print(result[2]) #second group (\(.*?\))

result:

t2 x Danish (Almond Danish)
t2 x Danish 
(Almond Danish)

Here is how you can achieve this

list1 = ['White Buns (Hot Dog)', ' 2 x Danish (Almond Danish) - A delight with coffee! \nThe best fruit fillings and of coarse butter filled flaky puff pastry.\n']

for word in list1:
  for i, letter in enumerate(word):
    if letter == ')':
      b.append(word[0:i+1])
print(b)

Output

['White Buns (Hot Dog)', ' 2 x Danish (Almond Danish)']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM