Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正则表达式从给定类型的字符串中获取多个日期_Python_Regex - Fatal编程技术网

Python 正则表达式从给定类型的字符串中获取多个日期

Python 正则表达式从给定类型的字符串中获取多个日期,python,regex,Python,Regex,我在我的Python程序中有类似的字符串,我根据请求添加了想要的结果: "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

我在我的Python程序中有类似的字符串,我根据请求添加了想要的结果:

"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") 
通过这两个正则表达式,我可以找到第一个字符串的3个日期:

(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})?)
是否可以使用一个或两个正则表达式模式从这些字符串中获取所有日期(以匹配所有日期)。因此,我认为它需要做的是:为每个日期查找下一个月的第一个月。如果没有给定,则获取相应的时间,如果后跟多个小时,则为每个日期创建多个日期时间。
格式没那么重要。

我没有足够的声誉来评论你的问题,但你不能只使用分组。在日数和月数部分加上括号,找出组号,并将其与小时分组部分放在一起?那么我想你只需要1个正则表达式,但是需要一点对组的处理。 这里有一个链接这里有一个分组的小例子第二个,非常简单,你可能已经知道这件事


向你致以亲切的问候

我让你开始了。这是我对你问题的解释。我将
parse_complex
的实现留给您

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')

如果您对我记录本课程的方式有疑问,请随时与我联系,我可以为您提供更多帮助。这个问题比我最初想的要复杂一点,我需要先做一些其他的事情。

应该是12月1日星期六+14小时吗?如果你为更多的日期写一些例子,我可以回答这个问题。我会根据我注意到的任何
+
符号或其他规则/模式拆分字符串并附加日期。在我看来,字符串解析通常更适合于数据处理。但我不能仅用一个例子来说明如何处理它。请更新,我将发布一个解决方案。我正在努力整合一个解决方案,以适应您为上一个示例提供的……不同寻常的……格式,
“Wed 12、Thu 13、Fri 14和Sat 15 Jun-19h+Sun 16 Jun-12h30”
+
的过载是一个特别的痛处。你没有更多的带有
s以及
+
符号的日期示例了,是吗?只有在.NET中才能获得任意数量的捕获(通过重用单个组)。在所有其他正则表达式引擎中(据我所知),重用的捕获组总是会覆盖以前捕获的内容。它不是正则表达式,但捕获异常可能也更好。你真的帮了我,谢谢!