python - 使用正则表达式删除 <a> 并仅从具有特定语言的 html 行返回其文本

标签 python regex

我正在尝试删除 <a>包含特定句子的行标签如下:

text before line im interested in which may include <a> tag </a>
Go to <a href="#step2"> Step 2</a>
text after line im intrested in which may also include <a> tag </a>

到目前为止我想到的是:

(?!(Go to|Return to|Continue to)( )?)(<a(.*)?>(?!(( )?Step \d( )?))(.*)?<\/a>)|(<a.*(Go to|Return to|Continue to).*\/a>)

但这似乎并不能满足我的需要:-( <a> tag </a>

期望的结果:Go to Step 2

我错过了什么?

最佳答案

我的猜测是,也许这个表达方式可能与您的想法很接近,但不确定。

使用re.findall进行测试

import re

regex = r"(go\s+to|return\s+to|continue\s+to)\s*<a\s+(?:[^>]+?)>([^<]+?)\s*</a>"

test_str = ("text before line im interested in which may include <a> tag </a>\n"
    "Go to <a href=\"#step2\"> Step 2</a>\n"
    "Return to <a href=\"#step2\"> Step 20 </a>\n"
    "CONTINUE To      <a href=\"#step2\"> Step 20   </a>\n"
    "text after line im intrested in which may also include <a> tag </a>")

matches = re.findall(regex, test_str, re.IGNORECASE)

for match in matches:
    print(match[0]+match[1])

输出

Go to Step 2
Return to Step 20
CONTINUE To Step 20

该表达式在 this demo 的右上角面板中进行了解释如果您想探索/简化/修改它。

关于python - 使用正则表达式删除 <a> 并仅从具有特定语言的 html 行返回其文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57045960/

相关文章:

Python 二维数组无法工作。帮忙~

python - 删除第一个 NaN 之后的 DataFrame 行

python - Sympy:在关系上使用逻辑运算符

python - OPENCV 如何补齐缺失的矩形?

c - C 预处理器宏中的正则表达式

regex - 为什么在我的正则表达式模式中使用 POSIX 字符类会产生意想不到的结果?

python - python中分类变量的knn插补

正则表达式从任何地方排除字符串,但匹配另一个表达式

javascript - 通过公共(public)分隔符解析 javascript 中的字符串

regex - sed正则表达式用同名变量替换值