python - python中的正则表达式,一个特例

标签 python regex

我是Python新手,我试图从字符串中提取一个值,但它不起作用。 我的字符串是这样的:

<a href=​"/​profile/​view?id=34232962&goback=%2Enmp_*1_*1_*1_*1_*1_*1_*1_*1_*1_*1&trk=spm_pic" title=​"View your profile">​

我的尝试是:

m = re.search('^.*\b(view|your|profile)\b.*$', newp, re.IGNORECASE)
print m.group(0)

所需的输出:

/​profile/​view?id=34232962&goback=%2Enmp_*1_*1_*1_*1_*1_*1_*1_*1_*1_*1 trk=spm_pic

最佳答案

正如您所发现的,正则表达式对于解析 HTML 来说非常糟糕。使用专为该工作构建的工具。如果是 python,请使用 BeautifulSoup。

soup = BeautifulSoup(html_doc)
profile_a = soup.find(title="View your profile")
link = profile_a['href']
print link
>> /​profile/​view?id=34232962&goback=%2Enmp_*1_*1_*1_*1_*1_*1_*1_*1_*1_*1 trk=spm_pic

关于python - python中的正则表达式,一个特例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17380267/

相关文章:

Java 正则表达式 : match whole word with word boundary

python - 正则表达式,re.compile,返回None而不是匹配

ruby-on-rails - Ruby .scan 方法使用正则表达式返回空

regex - 正则表达式搜索带有空格出现次数的字符串

python - 复制模型实例和相关模型的更好方法

python - 结合网格/包 Tkinter

python - Web2py SQLFORM.grid 与 executesql

python - 从半径为 R1 和 R2 的圆环内的图像中提取数据点

python:在子列表中定位元素

python - 正则表达式不能与 python 一起正常工作