gpt4 book ai didi

python - 转义不能修复 python 正则表达式错误 : unbalanced parenthesis

转载 作者:太空宇宙 更新时间:2023-11-04 03:43:48 28 4
gpt4 key购买 nike

我不是很精通 python,但从我在这里读到的内容来看,使用 re.escape() 应该可以修复不平衡的括号错误。但它不适用于我的代码。

谁能帮我解释一下?

    data_name = get_results(re.search('^(?!<br>).*(?=<br>)', pdf_data.group(0)))
data_name = re.escape(data_name)

示例 html 输出

    <span style="....">THING<br>ANOTHER TEXT THING<br></span>

我的代码的主要部分:

    # this is the pattern of the entire main span
text_pattern = '(?<=<span style="font-family: BALKHK+Arial,Bold; font-size:13px">(.*?)(?=<br></span></div>)'


def get_title():
# converted_html is the converted html file using pdfminer
for pdf_data in re.finditer(text_pattern, converted_html, re.DOTALL):

data_name = get_results(re.search('^(?!<br>).*(?=<br>)', pdf_data.group(0)))
data_name = re.escape(data_name)

test_list.append(data_name)

return test_list

最佳答案

我强烈建议使用解析器而不是正则表达式。

import bs4

txt = r'''<span style="....">THING<br>ANOTHER TEXT THING<br></span>'''
soup = bs4.BeautifulSoup(txt)

for span in [span for span in soup('span') if span.attrs['style'] == '''font-family: BALKHK+Arial,Bold; font-size:13px''']:
result = span.next_element

关于python - 转义不能修复 python 正则表达式错误 : unbalanced parenthesis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24831258/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com