Use stdlib re for dialogue matching

This commit is contained in:
Veronica Berglyd Olsen
2024-09-22 17:34:04 +02:00
parent 9c63f621a8
commit c08033d153
4 changed files with 33 additions and 71 deletions
+6 -17
View File
@@ -24,30 +24,19 @@ import re
import pytest
from PyQt5.QtCore import QRegularExpression
from novelwriter import CONFIG
from novelwriter.constants import nwUnicode
from novelwriter.text.patterns import REGEX_PATTERNS
def allMatches(regEx: QRegularExpression, text: str) -> list[list[str]]:
def allMatches(regEx: re.Pattern, text: str) -> list[list[str]]:
"""Get all matches for a regex."""
result = []
if isinstance(regEx, QRegularExpression):
itt = regEx.globalMatch(text, 0)
while itt.hasNext():
match = itt.next()
result.append([
(match.captured(n), match.capturedStart(n), match.capturedEnd(n))
for n in range(match.lastCapturedIndex() + 1)
])
else:
for match in re.finditer(regEx, text):
result.append([
(match.group(n), match.start(n), match.end(n))
for n in range((match.lastindex or -1) + 1)
])
for match in re.finditer(regEx, text):
result.append([
(match.group(n), match.start(n), match.end(n))
for n in range((match.lastindex or 0) + 1)
])
return result