使用regex匹配的非元字符

2024-09-27 00:16:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图搜索以下字符串中的一行字符

</Connector>

我写了以下代码。但似乎找不到这根弦。我知道这些不是元字符,因此应该自动匹配。还有,我查了一下是否有空位。但没用。谁知道我做错了什么

for line in wim_file:
    if re.findall("</Connector>",line):
        print('Word Found')
    else:
        print("Word Not Found!!")

注意:下面一行有另一个字符串不应该匹配。我需要在上面提到的字符串中匹配带“/”字符的精确字符串

        <Connector some text>

编辑:请在下面找到更多的文字行

     <Connector RefLabel="70100-01-L" Tolerance="1" UniqueID="WPWDH">     
    <Property authority="Design" name="PartNumber">H1BB</Property>
    <Property authority="Design" name="Part">89</Property>
    <Property authority="Design" name="ZTH">1</Property>
    <Property authority="Design" name="Base">WSS Class 3</Property>
    <Property authority="Design" name="PATHID">H1BB</Property>
  </CoordinatedEntity>
</Connector>

Tags: 字符串代码nameforconnectorlineproperty字符
1条回答
网友
1楼 · 发布于 2024-09-27 00:16:06
#assuming wim_file is a filepointer
for line in wim_file.readlines():
    if re.findall(".*</Connector>.*",line):
        print('Word Found')
    else:
        print("Word Not Found!!")

对regex稍加修改就得到了所需的行

相关问题 更多 >

    热门问题