p = re.compile(ur'''telephone[^<]*> # Looking for telephone
(?P<Telephone>[^<]+) # Capture telephone (all text up to the next tag)
|
streetAddress[^<]*> # Looking for streetAddress
(?P<Address>[^<]+) # Capture address (all text up to the next tag)
|
Pages[^<]*> # Looking for Pages
(?P<Pages>[^<]+) # Capture Pages (all text up to the next tag)''', re.IGNORECASE | re.VERBOSE)
test_str = "YOUR STRING"
print filter(None, [x.group("Telephone") for x in re.finditer(p, test_str)])
print filter(None, [x.group("Address") for x in re.finditer(p, test_str)])
print filter(None, [x.group("Pages") for x in re.finditer(p, test_str)])
考虑到您的输入不是有效的HTML并且它可能会发生更改,您可以使用类似BeautifulSoup的HTML解析器。这些简单的选择符将适应这些输入。在
编辑:既然您现在告诉我们您需要具有指定属性值的元素的文本,那么您可以use a function as filter。在
^{pr2}$输出:
Regex是安全的,如果你知道HTML提供者,里面的代码是什么样子的。在
然后,只需使用交替和命名的捕获组。在
见demo
如果
^{pr2}$>
未序列化,可以使用以下regex(更通用的regex,edit:现在,详细说明):Sample demo on IDEONE
粘贴regex代码部分:
输出(加倍的结果是我用不同的节点顺序复制输入字符串的结果):
相关问题 更多 >
编程相关推荐