Home > database >  Splitting String Methodically
Splitting String Methodically

Time:08-31

Given the string below, how can I best split the long string with times such as that I receive Productivity & Costs (Preliminary)(08:30) & then separately Consumer Credit Panel(11:00)

The following code depicts my long string:

['Productivity & Costs (Preliminary)(08:30)Consumer Credit Panel(11:00) ']

CodePudding user response:

The question is kind of confusing, but from what I can tell, you want to split your string into [‘Productivity & Costs (Preliminary)(08:30)’,’Consumer Credit Panel(11:00)’]

Assuming that you always want to split on ‘Consumer’, this should work:

import re
str_to_split = ‘Productivity & Costs (Preliminary)(08:30)Consumer Credit Panel(11:00)’
# str_arr is the list of the 2 strings
str_arr = re.split(‘(?=Consumer)’, str_to_split)

Again, I’m not sure what your end goal is here, so this answer is likely incomplete.

CodePudding user response:

data = 'Productivity & Costs (Preliminary)(08:30)Consumer Credit Panel(11:00) '
x = re.findall(r'.*?\(\d\d:\d\d\)', data)
  • .*? search for anything non-greedy
  • \(\d\d:\d\d\) find time, sort of (01:23)
  • re.findall(...) find all phrases like whatever the message (01:23)
  • Related