Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8844377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:28:18+00:00 2026-06-14T11:28:18+00:00

I have strings like these in my Python program and I added the wanted

  • 0

I have strings like these in my Python program and I added the wanted result as requested:

"Sat 1 Dec - 11h + 14h / Sun 2 Dec - 12h30"
("Sat 1 Dec 11h", "Sat 1 Dec 14h", "Sun 2 Dec 12h30")
"Tue 27 + Wed 28 Nov - 20h30"
("Tue 27 Nov 20h30", "Wed 28 Nov 20h30")
"Fri 4 + Sat 5 Jan - 20h30"
("Fri 4 Jan 20h30", "Sat 5 Jan 20h30")
"Wed 23 Jan - 20h"
("Wed 23 Jan 20h")
"Sat 26 Jan - 11h + 14h / Sun 27 Jan - 11h"
("Sat 26 Jan 11h", "Sat 26 Jan 14h", "Sun 27 Jan 11h")
"Fri 8 and Sat 9 Feb - 20h30 + thu 1 feb - 15h"
("Fri 8 Feb 20h30", "Sat 9 Feb 20h30", "Thu 1 feb 15h")
"Sat 2 Mar - 11h + 14h / Sun 3 Mar - 11h"
("Sat 2 Mar 11h", "Sat 2 Mar 14h", "Sun 3 Mar 11h")
"Wed 12, Thu 13, Fri 14 and Sat 15 Jun - 19h + Sun 16 Jun - 12h30"
("Wed 12 Jun 19h", "Thu 13 Jun 19h", "Fri 14 Jun 19h", "Sat 15 Jun 19h", "Sun 16 Jun 12h30") 

and with these two regex I can finde the 3 dates of the first string:

(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s([0-9]{1,2}\s(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))(?:.*?)([0-9]{1,2}[uh\:](?:[0-9]{2})?)

(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s([0-9]{1,2}\s(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))(?:.*?\+\s)([0-9]{1,2}[uh\:](?:[0-9]{2})?)

Is it possible to get all the dates from these strings with one or two regex patterns (to match all of them). So I think what it needs to do: finding the first following month for each date if not given, get the corresponding time and if followed by multiple hours make multiple datetimes per date.
Formatting is not that important.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:28:19+00:00Added an answer on June 14, 2026 at 11:28 am

    I got you started. This is my interpretation of your problem. I leave the implementation of parse_complex up to you.

    class DateParser(object):
        """parse dates according to the custom rules here:
    
        >>> DateParser("Sat 1 Dec - 11h + 14h / Sun 2 Dec - 12h30").parse()
        ("Sat 1 Dec 11h", "Sat 1 Dec 14h", "Sun 2 Dec 12h30")
        >>> DateParser("Tue 27 + Wed 28 Nov - 20h30").parse()
        ("Tue 27 Nov 20h30", "Wed 28 Nov 20h30")
        >>> DateParser("Fri 4 + Sat 5 Jan - 20h30").parse()
        ("Fri 4 Jan 20h30", "Sat 5 Jan 20h30")
        >>> DateParser("Wed 23 Jan - 20h").parse()
        ("Wed 23 Jan 20h")
        >>> DateParser("Sat 26 Jan - 11h + 14h / Sun 27 Jan - 11h").parse()
        ("Sat 26 Jan 11h", "Sat 26 Jan 14h", "Sun 27 Jan 11h")
        >>> DateParser("Fri 8 and Sat 9 Feb - 20h30 + thu 1 feb - 15h").parse()
        ("Fri 8 Feb 20h30", "Sat 9 Feb 20h30", "Thu 1 feb 15h")
        >>> DateParse("Sat 2 Mar - 11h + 14h / Sun 3 Mar - 11h").parse()
        ("Sat 2 Mar 11h", "Sat 2 Mar 14h", "Sun 3 Mar 11h")
        >>> DateParser("Wed 12, Thu 13, Fri 14 and Sat 15 Jun - 19h + Sun 16 Jun - 12h30").parse()
        ("Wed 12 Jun 19h", "Thu 13 Jun 19h", "Fri 14 Jun 19h", "Sat 15 Jun 19h", "Sun 16 Jun 12h30")
        """
    
        def __init__(self, line):
            self.date  = line
            self.dates = self.split_dates(line)
            self.final = []
    
            self.days = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
            self.mons = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
    
        def parse(self):
            if self.is_complex():
                self.parse_complex()
            else:
                self.parse_simple()
    
            return tuple(self.final)
    
        def parse_simple(self):
            """typical formats: 
            Day 00 + Day 01 Mon - 00h00
            Day 00 Mon - 00h00 + 01h00
            Day 00 Mon - 00h00 / Day 02 Mon - 00h00
            """
    
            for date in self.dates:
                mods = self.split_modifiers(date)
    
                date_mods = []
                for mod in mods:
                    if self.is_complete(mod):
                        #only *one* base_date
                        base_date, time = self.split_time(mod)
                        date_mods.append(time)
                    else:
                        date_mods.append(mod)
    
                for mod in date_mods:
                    if self.is_hour(mod):
                        #Sat 1 Dec - 11h + 14h
                        self.final.append(' '.join([base_date, mod]))
                    else:
                        #Fri 4 + Sat 5 Jan - 20h30
                        self.final.append(' '.join([mod, self.extract_month(base_date), time]))
    
        def parse_complex(self):
            """typical format:
            Day 00, Day 01 and Day 02 Mon - 00h00 + Day 03 Mon 01h00
            """
            raise NotImplementedError()
    
    
        def is_complex(self):
            """presence of the complex date attribute requires special parsing"""
            return self.date.find(' and ') > -1
    
        def is_complete(self, section):
            """section has format `Day 00 Mon - 00h00`
            must have no modifiers to determine completeness
            """
            sections = map(lambda x: x.lower(), section.split())
    
            try:
                dow, dom, moy, dash, time = sections
            except ValueError, e:
                return False
    
            return all([dow in self.days, moy in self.mons])
    
    
        def is_hour(self, section):
            return section[0].isdigit()
    
        def is_day(self, section):
            return section[:3].lower() in self.days
    
    
        def extract_month(self, section):
            """return the month present in the string, if any"""
            for mon in self.mons:
                if section.lower().find(mon) > -1:
                    found = section.lower().index(mon)
                    return section[found : found + 3]
            return None
    
    
        def split_dates(self, section):
            """split individual dates from a list of dates"""
            return section.split(' / ')
    
        def split_time(self, section):
            """split individual times from a complete date"""
            return section.split(' - ')
    
        def split_modifiers(self, section):
            """extend a date by implying that they share a date or a time
            modifiers change their meaning when parsing a complex date
            """
            return section.split(' + ')
    
    >>> DateParser("Fri 4 + Sat 5 Jan - 20h30 / Sat 1 Dec - 11h + 14h + 16h / Sun 2 Dec - 12h30").parse()
    ('Fri 4 Jan 20h30', 'Sat 5 Jan 20h30', 'Sat 1 Dec 11h', 'Sat 1 Dec 14h', 'Sat 1 Dec 16h', 'Sun 2 Dec 12h30')
    

    If you have questions about the way I’ve documented this class, feel free to get back to me, and I can help you out some more. This problem was a little more complex than I first thought, I need to get some other stuff done first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to have multiline strings in VB.NET like Python a =
I have strings like these: something something [[abcd]] blah blah something something [[xyz|abcd]] blah
I have strings that look like these: {server}_{date:YYYYMMDD}{int:######} {server}_{date:MON DAY YYYY}{int:######} ...plus more, in
I have strings like these: text-23 the-text-9 2011-is-going-to-be-cool-455 I need to remove the final
I have a string like val1_val2,val3_val4 and I need to split these values to
I have strings like this: http://localhost:2055/web-site-2009/paginas/noticias/**IGP-M recua 0,36% em agosto, aponta FGV**-46.aspx I'd like
I have strings like this: Car is blue String could also be like this:
I have strings like 84, 03 etc. that I want to convert to Date
I have strings like follow field_zip_code:48103 taxonomy:88 field_zip_code:48103 taxonomy:88 field_state:MI field_zip_code:48103 From here i
I have strings like: Avery® Laser & Inkjet Self-Adhesive I need to convert them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.