我有一些HTML,我要提取出的文本块是:多个正则表达式的工作不一行匹配
- 开始要么
#
或|
(管道符号),其次是一些文本和 - 括号
- “股票”,随后的所有文字,直到下一场比赛
示例代码:
text = """
#Test name 1 (ABCD) blah blah# some more text 1||Test name 2 (EFGH) blah blah some more text 2
#Test name 3 (IJKL) blah blah# some more text 3
|Test name 4 (MNOP) blah blah||some more text 4
|Test name 5 (QRST) blah blah||some more text 5|
"""
expr = r'(?P<alltext>(#|\|)[^<>]+\((?P<ticker>[A-Z]{1,10})\)(?P<bodytext>.*))'
compiled_expr = re.compile(expr, re.MULTILINE)
matches = re.finditer(expr,text)
for match in matches:
d=match.groupdict()
print d['alltext']
样本输出
#Test name 1 (ABCD) blah blah# some more text 1||Test name 2 (EFGH) blah blah some more text 2
#Test name 3 (IJKL) blah blah# some more text 3
|Test name 4 (MNOP) blah blah||some more text 4
|Test name 5 (QRST) blah blah||some more text 5|
这不拿起第一行的两场比赛。我需要的是为它来检测“测试名2 ......”
所以我想输出是:
#Test name 1 (ABCD) blah blah# some more text 1|
|Test name 2 (EFGH) blah blah some more text 2
#Test name 3 (IJKL) blah blah# some more text 3
|Test name 4 (MNOP) blah blah||some more text 4
|Test name 5 (QRST) blah blah||some more text 5|
根据您的标准,为什么'#一些文字1'不是一个单独的比赛? – thefourtheye 2014-10-01 18:18:49
@thefourtheye:这是因为'#some more text 1'中没有大括号'()'。 – 2014-10-01 18:20:42
您不需要多行修饰符。这个'[^ <>] +'贪婪地匹配你的字符串(全部)中的每个字符,因为你没有这些字符。结果,从第一个#到最后一组括号匹配,然后是括号,然后是其余。 – sln 2014-10-01 18:47:27