在Python中使用Regex提取多个匹配项

时间:2015-11-17 23:48:40

标签: python regex python-2.7

我是regex的新手,似乎无法弄清楚如何从字符串\nmedia_1.ts创建包含\nmedia_2.tss等匹配项的数组,如下所示。

关于如何改进正则表达式的任何建议?

s = '#EXTM3U\n#EXT-X-VERSION:3\n#EXT-X-TARGETDURATION:17\n#EXT-X-MEDIA-SEQUENCE:1\n#EXTINF:16.667,\nmedia_1.ts?wowzasessionid=184420502\n#EXTINF:16.666,\nmedia_2.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_3.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_4.ts?wowzasessionid=184420502\n#EXTINF:16.666,\nmedia_5.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_6.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_7.ts?wowzasessionid=184420502\n#EXTINF:16.666,\nmedia_8.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_9.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_10.ts?wowzasessionid=184420502\n#EXTINF:16.666,\nmedia_11.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_12.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_13.ts?wowzasessionid=184420502\n#EXTINF:16.666,\nmedia_14.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_15.ts?wowzasessionid=184420502'
pattern = re.compile('nmedia_(.*?).ts', re.IGNORECASE)
match = pattern.findall(s)
match

2 个答案:

答案 0 :(得分:1)

我建议您使用正则表达式声明尽可能明确。因此,我建议的改进是\d+匹配一个或多个数字而不是.*,零或更多。

由于您是regex的新手,因此您可能会对使用详细标记感兴趣。 (它也适用于文档。)这是一个代码示例:

# -*- coding: utf-8 -*-
import re

s = r'#EXTM3U\n#EXT-X-VERSION:3\n#EXT-X-TARGETDURATION:17\n#EXT-X-MEDIA-SEQUENCE:1\n#EXTINF:16.667,\nmedia_1.ts?wowzasessionid=184420502\n#EXTINF:16.666,\nmedia_2.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_3.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_4.ts?wowzasessionid=184420502\n#EXTINF:16.666,\nmedia_5.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_6.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_7.ts?wowzasessionid=184420502\n#EXTINF:16.666,\nmedia_8.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_9.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_10.ts?wowzasessionid=184420502\n#EXTINF:16.666,\nmedia_11.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_12.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_13.ts?wowzasessionid=184420502\n#EXTINF:16.666,\nmedia_14.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_15.ts?wowzasessionid=184420502'
pattern = r"""
\\        # Match literal backslash
nmedia_   # Match the text 'nmedia_'
(         # Begin capturing group 1.
\d+       # Match one or more digits.
)         # End capturing group 1.
\.        # Match a literal dot.
ts        # Match the text 'test'
"""
pattern_match = re.compile(pattern, re.VERBOSE)
match = pattern_match.findall(s)
print(match)
#['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15']

这是一个demo

答案 1 :(得分:0)

您需要删除捕获组,并且必须在正则表达式中使用\n换行符而不是n

pattern = re.compile(r'\nmedia_.*?\.ts', flags=re.IGNORECASE)
match = pattern.findall(s)

如果你只输了数字,

pattern = re.compile(r'\nmedia_(.*?)\.ts', flags=re.IGNORECASE)
match = pattern.findall(s)

示例:

>>> import re
>>> pattern = re.compile(r'\nmedia_(.*?)\.ts', flags=re.IGNORECASE)
>>> s = '#EXTM3U\n#EXT-X-VERSION:3\n#EXT-X-TARGETDURATION:17\n#EXT-X-MEDIA-SEQUENCE:1\n#EXTINF:16.667,\nmedia_1.ts?wowzasessionid=184420502\n#EXTINF:16.666,\nmedia_2.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_3.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_4.ts?wowzasessionid=184420502\n#EXTINF:16.666,\nmedia_5.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_6.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_7.ts?wowzasessionid=184420502\n#EXTINF:16.666,\nmedia_8.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_9.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_10.ts?wowzasessionid=184420502\n#EXTINF:16.666,\nmedia_11.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_12.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_13.ts?wowzasessionid=184420502\n#EXTINF:16.666,\nmedia_14.ts?wowzasessionid=184420502\n#EXTINF:16.667,\nmedia_15.ts?wowzasessionid=184420502'
>>> pattern.findall(s)
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15']
>>> pattern = re.compile(r'\nmedia_.*?\.ts', flags=re.IGNORECASE)
>>> pattern.findall(s)
['\nmedia_1.ts', '\nmedia_2.ts', '\nmedia_3.ts', '\nmedia_4.ts', '\nmedia_5.ts', '\nmedia_6.ts', '\nmedia_7.ts', '\nmedia_8.ts', '\nmedia_9.ts', '\nmedia_10.ts', '\nmedia_11.ts', '\nmedia_12.ts', '\nmedia_13.ts', '\nmedia_14.ts', '\nmedia_15.ts']
>>>