diff --git a/novelwriter/text/patterns.py b/novelwriter/text/patterns.py index 55d813f7c..8e5d107fb 100644 --- a/novelwriter/text/patterns.py +++ b/novelwriter/text/patterns.py @@ -28,7 +28,9 @@ from novelwriter import CONFIG from novelwriter.common import compact, uniqueCompact -from novelwriter.constants import nwRegEx +from novelwriter.constants import nwRegEx, nwUnicode + +AMBIGUOUS = (nwUnicode.U_APOS, nwUnicode.U_RSQUO) class RegExPatterns: @@ -92,11 +94,13 @@ def dialogStyle(self) -> re.Pattern | None: if CONFIG.dialogStyle in (1, 3): qO = CONFIG.fmtSQuoteOpen.strip()[:1] qC = CONFIG.fmtSQuoteClose.strip()[:1] - rx.append(f"(?:\\B{qO}.*?(?:{qC}\\B{end}))") + qB = r"\B" if (qO == qC or qC in AMBIGUOUS) else "" + rx.append(f"(?:{qO}.*?(?:{qC}{qB}{end}))") if CONFIG.dialogStyle in (2, 3): qO = CONFIG.fmtDQuoteOpen.strip()[:1] qC = CONFIG.fmtDQuoteClose.strip()[:1] - rx.append(f"(?:\\B{qO}.*?(?:{qC}\\B{end}))") + qB = r"\B" if (qO == qC or qC in AMBIGUOUS) else "" + rx.append(f"(?:{qO}.*?(?:{qC}{qB}{end}))") return re.compile("|".join(rx), re.UNICODE) return None @@ -106,7 +110,8 @@ def altDialogStyle(self) -> re.Pattern | None: if CONFIG.altDialogOpen and CONFIG.altDialogClose: qO = re.escape(compact(CONFIG.altDialogOpen)) qC = re.escape(compact(CONFIG.altDialogClose)) - return re.compile(f"\\B{qO}.*?{qC}\\B", re.UNICODE) + qB = r"\B" if (qO == qC or qC in AMBIGUOUS) else "" + return re.compile(f"{qO}.*?{qC}{qB}", re.UNICODE) return None