0

I have a string with brackets and all sorts of characters and would like to extract everything of this string up to the word price (without price). I have tried the following but I just can't make it work.

string = 'b012500 GBP 6,500 (change) price'
re.search('^[^price]+',string)

I would like to get this as output

b012500 GBP 6,500 (change) 

How can I do this?

corianne1234
  • 634
  • 9
  • 23
  • It is `r'^.*?(?=price)'` in general. You may enhance it any way you want, like `r'^.*?(?=\s*price)'` or `r'^.*?(?=\s+price\b)'` – Wiktor Stribiżew Apr 11 '21 at 18:25
  • Thanks a lot, but if I try this for example `string = '\n280 PS (206 kW), gas\n power\n' re.findall(r'^.*?(?=\s*gas)',string)` then it doesn't work to get everything before `gas` – corianne1234 Apr 11 '21 at 18:44
  • 1
    It is a common issue, use `re.findall(r'(?s)^.*?(?=\s*gas)',string)` or `re.findall(r'^.*?(?=\s*gas)',string, re.S)`, or `re.findall(r'^[\w\W]*?(?=\s*gas)',string)`, etc. – Wiktor Stribiżew Apr 11 '21 at 18:45
  • is there a way to use f strings with that as well? If I want to be able to put in many different strings? Or something like this r'(?s)^.*?(?=\s*%s)' % (names[k]) – corianne1234 Apr 11 '21 at 22:19
  • See https://stackoverflow.com/questions/6930982/how-to-use-a-variable-inside-a-regular-expression – Wiktor Stribiżew Apr 11 '21 at 22:47

0 Answers0