Python 中用正则表达式替换字符串
问题:
如何替换 HTML使用正则表达式在字符串中标记Python?
输入:
this is a paragraph with<[1]> in between</[1]> and then there are cases ... where the<[99]> number ranges from 1-100</[99]>. and there are many other lines in the txt files with<[3]> such tags </[3]>
所需输出:
this is a paragraph with in between and then there are cases ... where the number ranges from 1-100. and there are many other lines in the txt files with such tags
解决方案:
使用正则表达式替换多个标签Python,按照以下步骤操作:
import re line = re.sub(r"<\/?\[\d+>]", "", line)
说明:
正则表达式 r"?[d >"] 匹配以任何开头的标签 结尾。问号字符? / 后面表示斜杠是可选的。 sub 函数将每个匹配项替换为空字符串。
注释版本:
line = re.sub(r""" (?x) # Use free-spacing mode. < # Match a literal '<' /? # Optionally match a '/' \[ # Match a literal '[' \d+ # Match one or more digits > # Match a literal '>' """, "", line)
附加注释:
以上是如何使用 Python 正则表达式从字符串中删除 HTML 标签?的详细内容。更多信息请关注PHP中文网其他相关文章!