Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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_Python 2.7 - Fatal编程技术网

提取日期模式的Python正则表达式

提取日期模式的Python正则表达式,python,regex,python-2.7,Python,Regex,Python 2.7,我有一个这样的句子(关键字后跟左括号,后跟任意字符串,后跟两个日期,以连字符分隔): 我需要使用正则表达式从这个句子中提取出生日期(1869年10月2日)和死亡日期(1948年1月30日)。我已经编写了正则表达式来提取日期模式 date_pattern="(\d{1,2}(\s|-|/)?(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May?|June?|July?|Aug(ust)?|Sep(t(ember)?)?|Oct(ober)?|Nov(ember

我有一个这样的句子(关键字后跟左括号,后跟任意字符串,后跟两个日期,以连字符分隔):

我需要使用正则表达式从这个句子中提取出生日期(1869年10月2日)和死亡日期(1948年1月30日)。我已经编写了正则表达式来提取日期模式

date_pattern="(\d{1,2}(\s|-|/)?(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May?|June?|July?|Aug(ust)?|Sep(t(ember)?)?|Oct(ober)?|Nov(ember)?|Dec(ember)?|\d{1,2})(\s|-|/)?\d{2,4})"
我需要提取上述表格中的句子,并分别打印出生日期和死亡日期

import re
date_pattern="(\d{1,2}(?:\s|-|/)?(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May?|June?|July?|Aug(?:ust)?|Sep(?:t(?:ember)?)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?|\d{1,2})(?:\s|-|/)?\d{2,4})"

bio = "Mohandas Karamchand Gandhi (/ˈɡɑːndi, ˈɡæn-/; Hindustani: [ˈmoːɦənd̪aːs ˈkərəmtʃənd̪ ˈɡaːnd̪ʱi]; 2 October 1869 – 30 January 1948) was the preeminent leader of the Indian independence movement in British-ruled India."

matches = re.findall(date_pattern, bio)
if matches and len(matches) > 1:
   born = matches[0]
   died = matches[1]
   print("Born:", born)
   print("Died:", died)
输出:


这是一个“为我调试我的代码”类型的问题,还是您正在努力解决的问题的特定部分?日期是多种不同的格式,还是只有一种特定的格式?你必须担心大写和小写吗?你在哪里卡住了,哪里没有解决你的问题?如果输入格式和你问题中的例子一样规则,你根本不需要正则表达式。每个信息都用括号和分号表示,因此您可以使用
str.split
获取部分,然后使用
datetime.strtime
解析日期。嗨,我需要匹配fom:关键字(甘地)的整个句子后跟左括号,后跟任意字符串,后跟2个日期,以连字符分隔,然后提取日期。这是一个例句。这句话中可能有许多其他日期。所以我需要提取满足上述条件的句子,然后提取出生和死亡日期,关键字后面跟左括号,后面跟任意字符串,后面跟两个用连字符分隔的日期
import re
date_pattern="(\d{1,2}(?:\s|-|/)?(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May?|June?|July?|Aug(?:ust)?|Sep(?:t(?:ember)?)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?|\d{1,2})(?:\s|-|/)?\d{2,4})"

bio = "Mohandas Karamchand Gandhi (/ˈɡɑːndi, ˈɡæn-/; Hindustani: [ˈmoːɦənd̪aːs ˈkərəmtʃənd̪ ˈɡaːnd̪ʱi]; 2 October 1869 – 30 January 1948) was the preeminent leader of the Indian independence movement in British-ruled India."

matches = re.findall(date_pattern, bio)
if matches and len(matches) > 1:
   born = matches[0]
   died = matches[1]
   print("Born:", born)
   print("Died:", died)
import re

text = '''Mohandas Karamchand Gandhi (/ˈɡɑːndi, ˈɡæn-/; Hindustani: [ˈmoːɦənd̪aːs ˈkərəmtʃənd̪ ˈɡaːnd̪ʱi]; 2 October 1869 – 30 January 1948) was the preeminent leader of the Indian independence movement in British-ruled India.'''
birth, death = re.findall(r'\d+[ \d\w]+', text)
print(birth)
print(death)
2 October 1869 
30 January 1948