diff --git a/novelwriter/constants.py b/novelwriter/constants.py index 1905c856..bf4f58c6 100644 --- a/novelwriter/constants.py +++ b/novelwriter/constants.py @@ -23,8 +23,6 @@ along with this program. If not, see . """ from __future__ import annotations -from re import UNICODE, compile - from PyQt5.QtCore import QT_TRANSLATE_NOOP, QCoreApplication from novelwriter.enum import ( @@ -62,16 +60,13 @@ class nwConst: class nwRegEx: + WORDS = r"\b[^\s\-\+\/–—\[\]:]+\b" FMT_EI = r"(? re.Pattern: + """Split text into words.""" + return self._rxWords + @property def markdownItalic(self) -> re.Pattern: """Markdown italic style.""" diff --git a/tests/test_text/test_text_patterns.py b/tests/test_text/test_text_patterns.py index 48f1e4c8..7f1def90 100644 --- a/tests/test_text/test_text_patterns.py +++ b/tests/test_text/test_text_patterns.py @@ -40,6 +40,52 @@ def allMatches(regEx: re.Pattern, text: str) -> list[list[str]]: return result +@pytest.mark.core +def testTextPatterns_Words(): + """Test the word split regex.""" + regEx = REGEX_PATTERNS.wordSplit + + # Spaces + assert allMatches(regEx, "one two three") == [ + [("one", 0, 3)], [("two", 4, 7)], [("three", 8, 13)] + ] + + # Hyphens + assert allMatches(regEx, "one-two-three") == [ + [("one", 0, 3)], [("two", 4, 7)], [("three", 8, 13)] + ] + + # Em Dashes + assert allMatches(regEx, "one\u2014two\u2014three") == [ + [("one", 0, 3)], [("two", 4, 7)], [("three", 8, 13)] + ] + + # Em Dashes + assert allMatches(regEx, "one\u2014two\u2014three") == [ + [("one", 0, 3)], [("two", 4, 7)], [("three", 8, 13)] + ] + + # Plus + assert allMatches(regEx, "one+two+three") == [ + [("one", 0, 3)], [("two", 4, 7)], [("three", 8, 13)] + ] + + # Slash + assert allMatches(regEx, "one/two/three") == [ + [("one", 0, 3)], [("two", 4, 7)], [("three", 8, 13)] + ] + + # Brackets + assert allMatches(regEx, "one[two]three") == [ + [("one", 0, 3)], [("two", 4, 7)], [("three", 8, 13)] + ] + + # Colon + assert allMatches(regEx, "one:two:three") == [ + [("one", 0, 3)], [("two", 4, 7)], [("three", 8, 13)] + ] + + @pytest.mark.core def testTextPatterns_Markdown(): """Test the markdown pattern regexes."""