Python正则表达式并不匹配所需的所有字符

Python正则表达式并不匹配所需的所有字符,python,regex,Python,Regex,我有一些由PDF制作的txt文件,希望使用一些python脚本和正则表达式模式添加一些xml标记。大多数情况下,它工作正常,但有时表达式并不匹配所需的所有字符。在测试工具中,它工作正常 下面是python代码: matchs = re.finditer("<UTop>[^<]+",string) for m in matchs: tagend = m.end() string = string[:tagend] + "</UTop&

我有一些由PDF制作的txt文件,希望使用一些python脚本和正则表达式模式添加一些xml标记。大多数情况下,它工作正常,但有时表达式并不匹配所需的所有字符。在测试工具中,它工作正常

下面是python代码:

matchs = re.finditer("<UTop>[^<]+",string)
    for m in matchs:
        tagend = m.end()
        string = string[:tagend] + "</UTop>" + string[tagend:]
matchs=re.finditer(“[^使用Unicode标志:

matchs = re.finditer("<UTop>[^<]+",string,re.UNICODE)
matchs=re.finditer(“[^我使用测试它,结果似乎是正确的

 #coding: utf-8
 import re
 input = "<Top>1. Regierungserklärung des Ministerpräsidenten<UTop>Ministerpräsident Winfried Kretschmann </Top>"
 print(re.sub(r"(<UTop>[^<]+)","\g<1><\\UTop>" ,input))
#编码:utf-8
进口稀土
input=“1.管理迷你解释和迷你解释Winfried Kretschmann”

print(re.sub(r)([^如果您试图使用正则表达式解析HTML,请使用+1表示BeautifulSoup。另请参阅以获取有关处理损坏的HTML输入的详细信息。感谢您的回答。不幸的是,unicode标志无法解决此问题。
<Top>1. Regierungserklärung des Ministerpräsidenten<UTop>Ministerpräsident Winfried Krets</UTop>chmann </Top>
matchs = re.finditer("<UTop>[^<]+",string,re.UNICODE)
 #coding: utf-8
 import re
 input = "<Top>1. Regierungserklärung des Ministerpräsidenten<UTop>Ministerpräsident Winfried Kretschmann </Top>"
 print(re.sub(r"(<UTop>[^<]+)","\g<1><\\UTop>" ,input))