简体   繁体   English

Python Regex文字和时间在同一行

[英]Python Regex text and time same line

I'm trying to grab a NHL schedule, its format is: 我正在尝试获取NHL时间表,其格式为:

    tabindex="1" >Game: Wild at Blackhawks 8:00PM ET</a>

I can grab it like: 我可以像这样抓住它:

    tabindex="1" >Game: (.)(.*?) at (.)(.*?)</a>

That Grabs the first letter of the team names then the rest (coloring purposes) plus the start time. 这样就可以抓住团队名称的第一个字母,然后是其余的(着色目的)加上开始时间。

I have tried variations of the following from posts I've found in here: 我尝试从以下文章中找到以下内容的变体:

    (?:(?:(\d+):)?(\d+):)?(.+?)$ (ET|PT)

However I would like to grab the Time, AM/PM, and ET separately for coloring as well. 但是,我也想分别获取时间,AM / PM和ET进行着色。 What stumps me is to figure out how to grab Text and Date following eachother in a line. 让我感到困扰的是弄清楚如何在一行中紧随彼此而获取文本和日期。 Any help would be appreciated, Thanks 任何帮助,将不胜感激,谢谢

Try below to grab Time, AM/PM, and ET or PT- 请尝试以下操作,以获取时间,上午/下午以及ET或PT-

>>>re.findall(r'([\d:]*)([A-Z]+)\s*([A-Z]+)(?=</a>)','tabindex="1" >Game: Wild at Blackhawks 8:00PM ET</a>')
>>>[('8:00', 'PM', 'ET')]

See LIVEDEMO LIVEDEMO

How about this: 这个怎么样:

>>> s = 'tabindex="1" >Game: Wild at Blackhawks 8:00PM ET</a>'
>>> m = re.search(r'tabindex="1" >Game: (\w)(\w*?) at (\w)(\w*?) (\d+:\d\d)(\w\w) (\w\w)</a>', s)
>>> m.groups(0)
('W', 'ild', 'B', 'lackhawks', '8:00', 'PM', 'ET')

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

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