首页 > 解决方案 > 使用 re.finditer 不会返回所有匹配项

问题描述

我一直在用python制作一个简单的着色器。它使用 re.finditer 查找引号之间所有单词的索引,并在 tkinter 文本框中为这些单词着色。出于某种原因,当盒子打开时,并没有找到所有的单词。这是我的代码:

import tkinter as tk
import re

def htmlbasiccolorer(self):
        def find2(self, color, warning):
                    string = (str(self.get("1.0",tk.END)))
                    lines=string.split("\n")
                    for i,line in enumerate(lines):
                                y=(i+1)
                                for e in re.finditer(r'"(.*?)"', line):
                                    startindex= e.start()
                                    endindex= e.end()
                                    startindex=(str(y)+'.'+(str(startindex)))
                                    endindex=(str(y)+'.'+(str(endindex)))
                                    startindex=float(startindex)
                                    endindex=float(endindex)
                                    startindex=(round(float(startindex), 2))
                                    endindex=(round(float(endindex), 2))
                                    self.tag_configure(warning, background="white", foreground=color)
                                    self.tag_add(warning, startindex, endindex)
        find2(self, "purple", "id-6")
s=tk.Tk()
s.geometry('1000x600')
t=tk.Text(s)
t.insert(tk.END, """
<!DOCTYPE html>
<html
  xmlns="http://www.w3.org/1999/xhtml"
  xml:lang="en-US"
  lang="en-US"
  dir="ltr"
  xmlns:fb="http://ogp.me/ns/fb#" xmlns:og="http://ogp.me/ns#"
  class=" user-logged-out">

<head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# game: http://ogp.me/ns/game#">
        <meta charset="utf-8" />
    <meta name="ROBOTS" content="NOODP" />
    <meta name="ROBOTS" content="NOYDIR" />
        <meta name="verify-v1" content="TgxixMKtxcQ+9NUdD7grKbzw3tAl3iJWlTPSPKt9t0I=" />
    <meta name="p:domain_verify" content="314c7ba9469cc171a12a46b43e0e2aed" />
    <meta name="google-site-verification" content="n7BdKb0xn1E9tRJXvmMxE3Ynr-QajBOi1yA1srT4Nrc" />
    <meta name="apple-itunes-app" content="app-id=329218549">

              <meta name="description" content="Play chess on Chess.com - the #1 chess community with +20 million members around the world. Play online with friends, challenge the computer, join a club, solve puzzles, analyze your games, and learn from hundreds of video lessons. You can also watch top players and compete for prizes." />

        <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">

    <link rel="preconnect" href="//betacssjs.chesscomfiles.com">
  <link rel="preconnect" href="//images.chesscomfiles.com">

    <link rel="dns-prefetch" href="//betacssjs.chesscomfiles.com">
  <link rel="dns-prefetch" href="//images.chesscomfiles.com">

    <link
    as="font"
    crossorigin="crossorigin"
    href="/bundles/web/fonts/chessglyph-regular.d4a95b80.woff2"
    rel="preload"
    type="font/woff2">

    <link rel="publisher" href="https://plus.google.com/+chess"/>

    <link rel="apple-touch-icon" href="https://betacssjs.chesscomfiles.com
    <div id="challenge-popover"></div>
    <div id="message-popover"></div>
    <div id="modal-video"></div>
    <div id="trophy-popover"></div>
    <div id="user-popover"></div>
    </body>
</html>

""")
t.pack(expand=1, fill=tk.BOTH)
htmlbasiccolorer(t)
s.mainloop()

下面是它的外观示例。紫色文字已找到,黑色文字未找到。两个引号之间的某些文本仍然是黑色的。 在此处输入图像描述 我在 Windows 10 上使用 python 3.6。任何帮助将不胜感激。

标签: pythonregexpython-3.xtkintertext-coloring

解决方案


您正在为文本小部件索引使用浮点数。索引不是浮点数,它们是格式line的字符串。。然后,您将做出向上或向下舍入索引的奇怪选择。

让我们以“NOYDIR”为例,因为您声称它没有找到。只需一条打印语句,您就会看到它正在查找 NOYDIR,但它正在计算的索引是 14.32 作为开始,14.4 作为结束。因为结束索引在开始索引之前(字符 4 在字符 32 之前),tkinter 不会突出显示该单词。

为什么第二个指数是 14.4?这是因为e.start()返回 40。您可以通过附加“。”将其转换为浮点数。和行的值,产生“1.40”。然后将其转换为浮点数,将“1.40”转换为“1.4”。这正是您不应将文本小部件索引视为浮点数的原因。索引是line形式的字符串。当您将其转换为浮点数时,值“14.40”与“14.4”相同,但对于文本小部件,“14.40”和“14.4”是非常不同的东西。


推荐阅读