简体   繁体   English

Python Regex 从字符串中提取位置和时间戳

[英]Python Regex to extract Location and Timestamp from string

I am a complete newbie to Python and after any help possible.我是 Python 的完全新手,并且在任何可能的帮助之后。 Below is sample text string which I am trying to extract 2 substrings:下面是我试图提取 2 个子字符串的示例文本字符串:

  1. Location地点
  2. Timestamp时间戳

Sample text: Your booking at Crown Street - June 29th, 1:00pm示例文本:您在 Crown Street 的预订 - 6 月 29 日下午 1 点

The Location substring is between the following 2 phrases were are constant "Your booking at " and " -". Location子字符串在以下 2 个短语之间是不变的“您的预订地点”和“-”。 The spaces includes in the phrases are deliberate.短语中包含的空格是故意的。 In this example, my required output string is Crown Street .在这个例子中,我需要的输出字符串是Crown Street What is the best Python regex to deliver this outcome?提供此结果的最佳 Python 正则表达式是什么?

The Timestamp substring procedes "- " expression in the string. Timestamp子字符串处理字符串中的“-”表达式。 In this example, my required output string is June 29th, 1:00pm .在这个例子中,我需要的输出字符串是June 29th, 1:00pm What is the best Python regex to deliver this outcome?提供此结果的最佳 Python 正则表达式是什么?

import re

example = 'Your booking at Crown Street - June 29th, 1:00pm'
regex = re.compile(r'Your booking at (?P<location>.+) - (?P<timestamp>.+)$')
print(regex.match(example).groupdict())

outputs产出

{'location': 'Crown Street', 'timestamp': 'June 29th, 1:00pm'}

Notice that this could end up in a false match if there's a - in the name of the location;请注意,如果位置名称中有- ,这可能会导致错误匹配; if you're always sure there'll be an English month to start the timestamp, you could use (?P<timestamp>(?:Jan|Feb|Mar|...).+) .如果你总是确定会有一个英语月份来开始时间戳,你可以使用(?P<timestamp>(?:Jan|Feb|Mar|...).+)

Using re.search使用re.search

Demo:演示:

import re
text = "Your booking at Crown Street - June 29th, 1:00pm"

data = re.search("Your booking at\s+(.*)\s+\-\s+(.*)", text)
if data:
    print(data.group(1))
    print(data.group(2))

Output:输出:

Crown Street
June 29th, 1:00pm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM