问题描述
我有这样的字符串:
{server}_{date:YYYYMMDD}{int:######}{server}_{date:MON DAY YYYY}{int:######}
...加上更多,以不同的日期格式.此外,可以有任意数量的 {} 块,它们可以按任意顺序出现.
我试图在 Python 3.2 中只获取大括号之间的日期"部分.因此,对于第一个字符串,我只想获取{date:YYYYMMDD}",而对于第二个字符串,我只想获取{date:MON DAY YYYY}".日期"块中我想要的唯一字符是字母和空格.
我的正则表达式模式是:
\{date:(\w|\s)*\}
我已经在 这个 Regex builder 上对此进行了测试,但它不是符合预期.这是我在 Python 上的输出:
>>>进口重新>>>re.findall('\{date:(\w|\s)*\}', '{server}_{date:YYYYMMDD}{date:MONDAYYYYY}{int:######}')['D', 'Y']>>>re.findall('\{date:(\w|\s)*\}', '{server}_{date:MON DAY YYYY}{int:######}')['是']有人可以指出我的模式有什么问题吗?
'(\{date:[\w\s]+\})'
给出你想要的:
如果你只想要数据值,使用'\{date:([\w\s]+)\}'
.
I have strings that look like these:
{server}_{date:YYYYMMDD}{int:######}
{server}_{date:MON DAY YYYY}{int:######}
...plus more, in different date formats. Also, there can be any number of {} blocks, and they can appear in any order.
I'm trying to get just the "date" part between the curly braces in Python 3.2. So for the first string, I want to get just "{date:YYYYMMDD}" and for the second string I want just "{date:MON DAY YYYY}". The only characters I want inside the "date" block are alpha and whitespace.
My regex pattern is:
\{date:(\w|\s)*\}
I've tested this out on this Regex builder, but it's not matching as expected. This is my output on Python:
>>> import re
>>> re.findall('\{date:(\w|\s)*\}', '{server}_{date:YYYYMMDD}{date:MONDAYYYYY}{int:######}')
['D', 'Y']
>>> re.findall('\{date:(\w|\s)*\}', '{server}_{date:MON DAY YYYY}{int:######}')
['Y']
Can someone please point out what's wrong with my pattern?
'(\{date:[\w\s]+\})'
gives what you want:
>>> import re
>>> re.findall('(\{date:[\w\s]+\})', '{server}_{date:YYYYMMDD}{date:MONDAYYYYY}{int:######}')
['{date:YYYYMMDD}', '{date:MONDAYYYYY}']
>>> re.findall('(\{date:[\w\s]+\})', '{server}_{date:MON DAY YYYY}{int:######}')
['{date:MON DAY YYYY}']
If you want only data value, use '\{date:([\w\s]+)\}'
.
这篇关于Python正则表达式提取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!