需要帮助创建一个在python中使用的正则表达式

时间:2013-10-16 01:35:10

标签: regex python-2.7

以下是该页面的摘录:

<tr id="product_34980" class="even">
<tr id="variant_100329" class="variantRow">

我想提取34980和100329.可能有多种产品和变体。我将使用python。

由于

2 个答案:

答案 0 :(得分:0)

链接@Kirill Polishchuk给出了SO的最爱,它清楚地说明了为什么你不应该使用正则表达式。

但是,如果仍然坚持使用正则表达式,请尝试:

<tr[^>]*id="([^"]*)"[^>]*>

您的匹配现在位于捕获组#1

答案 1 :(得分:0)

>>> p = re.compile('\d+')
>>> m = re.search(p, '<tr id="product_34980" class="even">')
>>> m.group()
'34980'