简体   繁体   English

如何在 Python 中找到带有正则表达式的字符串

[英]How can I find string with regular expressions in Python

I have html string like that for example例如,我有这样的 html 字符串

<td align="left" nowrap="nowrap">John 23</td>

I want to find "John 23" between '<td align="left" nowrap="nowrap">' and '</td>'我想在'<td align="left" nowrap="nowrap">''</td>'之间找到“John 23”

I want to find with Regular Expressions in python我想在 python 中使用正则表达式查找

How can I do it?我该怎么做?

Use BeautifulSoup to parse HTML.使用 BeautifulSoup 解析 HTML。 Regex is the wrong tool;正则表达式是错误的工具; it works fine for this example but wouldn't scale well to a full document.它适用于这个例子,但不能很好地扩展到一个完整的文档。

>>> from bs4 import BeautifulSoup
>>> html = '<td align="left" nowrap="nowrap">John 23</td>'
>>> BeautifulSoup(html).find("td").text
'John 23'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何找到一个字符串,其中包含两个正则表达式之间的任意字符串,但python中的某个正则表达式除外? - How can I find a string that contains any between two regular expressions except for a certain regex in python? 如何使用 Python 中的正则表达式在字符串中查找重复的 substring? - How to find repeated substring in a string using regular expressions in Python? 我可以在python中用几个正则表达式的并集扩展单个字符串吗? - Can I expand a single string with the union of several regular expressions in python? 正则表达式:使用Python在String中查找名称 - Regular Expressions: Find Names in String using Python 如何测试字符串是否引用文件或目录? 用正则表达式? 在蟒蛇? - How can I test if a string refers to a file or directory? with regular expressions? in python? 如何在python正则表达式中找到字符串的特定部分? - How can i find specific part of string in python regular expression? 如何使用正则表达式找到所有 Markdown 链接? - How can I find all Markdown links using regular expressions? 如何使用正则表达式删除python字符串中的十六进制值? - How do I remove hex values in a python string with regular expressions? 如何使用正则表达式在字符串中查找重复模式? - How to find repetitive patterns in a string with regular expressions? 如何使用Python改变正则表达式中的替换字符串? - How to vary the substituion string in regular expressions with Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM