import re
from pyqode.core.qt import QtGui


class MatchHighlighter(QtGui.QSyntaxHighlighter):
    def __init__(self, document):
        super().__init__(document)
        self.prog = None
        self._format = QtGui.QTextCharFormat()
        self._format.setBackground(QtGui.QBrush(QtGui.QColor('#bbfcbb')))

    def highlightBlock(self, text):
        if self.prog and text:
            match = self.prog.search(text)
            if match:
                start, end = match.span()
                while match and end > start:
                    self.setFormat(start, end - start, self._format)
                    match = self.prog.search(text, match.end())
                    if match:
                        start, end = match.span()


import re
from pyqode.core.qt import QtGui


class MatchHighlighter(QtGui.QSyntaxHighlighter):
    def __init__(self, document):
        super().__init__(document)
        self.prog = None
        self._format = QtGui.QTextCharFormat()
        self._format.setBackground(QtGui.QBrush(QtGui.QColor('#bbfcbb')))

    def highlightBlock(self, text):
        if self.prog and text:
            for m in self.prog.finditer(text):
                start, end = m.span()
                self.setFormat(start, end - start, self._format)

Fix highlighting when multiple matches
Fix highlighting when multiple matches

  import re
  from pyqode.core.qt import QtGui
  
  
  class MatchHighlighter(QtGui.QSyntaxHighlighter):
      def __init__(self, document):
          super().__init__(document)
          self.prog = None
          self._format = QtGui.QTextCharFormat()
          self._format.setBackground(QtGui.QBrush(QtGui.QColor('#bbfcbb')))
  
      def highlightBlock(self, text):
          if self.prog and text:
+             for m in self.prog.finditer(text):
-             match = self.prog.search(text)
-             if match:
-                 start, end = match.span()
+                 start, end = m.span()
-                 while match and end > start:
-                     self.setFormat(start, end - start, self._format)
+                 self.setFormat(start, end - start, self._format)
-                     match = self.prog.search(text, match.end())
-                     if match:
-                         start, end = match.span()
  
- 
Fix highlighting when multiple matches
## Code Before:
import re
from pyqode.core.qt import QtGui


class MatchHighlighter(QtGui.QSyntaxHighlighter):
    def __init__(self, document):
        super().__init__(document)
        self.prog = None
        self._format = QtGui.QTextCharFormat()
        self._format.setBackground(QtGui.QBrush(QtGui.QColor('#bbfcbb')))

    def highlightBlock(self, text):
        if self.prog and text:
            match = self.prog.search(text)
            if match:
                start, end = match.span()
                while match and end > start:
                    self.setFormat(start, end - start, self._format)
                    match = self.prog.search(text, match.end())
                    if match:
                        start, end = match.span()


## Instruction:
Fix highlighting when multiple matches
## Code After:
import re
from pyqode.core.qt import QtGui


class MatchHighlighter(QtGui.QSyntaxHighlighter):
    def __init__(self, document):
        super().__init__(document)
        self.prog = None
        self._format = QtGui.QTextCharFormat()
        self._format.setBackground(QtGui.QBrush(QtGui.QColor('#bbfcbb')))

    def highlightBlock(self, text):
        if self.prog and text:
            for m in self.prog.finditer(text):
                start, end = m.span()
                self.setFormat(start, end - start, self._format)
