Add URL regex pattern

This commit is contained in:
Veronica Berglyd Olsen
2024-10-25 17:49:53 +02:00
parent 439a04c603
commit 37268b873a
3 changed files with 53 additions and 2 deletions
+1
View File
@@ -60,6 +60,7 @@ class nwConst:
class nwRegEx:
URL = r"https?://(?:www\.|(?!www))[\w/()@:%_\+-.~#?&=]+"
WORDS = r"\b[^\s\-\+\/–—\[\]:]+\b"
BREAK = r"(?i)(?<!\\)(\[br\]\n?)"
FMT_EI = r"(?<![\w\\])(_)(?![\s_])(.+?)(?<![\s\\])(\1)(?!\w)"
+6
View File
@@ -32,6 +32,7 @@ from novelwriter.constants import nwRegEx
class RegExPatterns:
# Static RegExes
_rxUrl = re.compile(nwRegEx.URL, re.ASCII)
_rxWords = re.compile(nwRegEx.WORDS, re.UNICODE)
_rxBreak = re.compile(nwRegEx.BREAK, re.UNICODE)
_rxItalic = re.compile(nwRegEx.FMT_EI, re.UNICODE)
@@ -40,6 +41,11 @@ class RegExPatterns:
_rxSCPlain = re.compile(nwRegEx.FMT_SC, re.UNICODE)
_rxSCValue = re.compile(nwRegEx.FMT_SV, re.UNICODE)
@property
def url(self) -> re.Pattern:
"""Find URLs."""
return self._rxUrl
@property
def wordSplit(self) -> re.Pattern:
"""Split text into words."""
+46 -2
View File
@@ -40,6 +40,48 @@ def allMatches(regEx: re.Pattern, text: str) -> list[list[str]]:
return result
@pytest.mark.core
def testTextPatterns_Urls():
"""Test the URL regex."""
regEx = REGEX_PATTERNS.url
valid = [
"http://example.com",
"http://example.com/",
"http://example.com/path+to+page",
"http://example.com/path-to-page",
"http://example.com/path_to_page",
"http://example.com/path~to~page",
"http://example.com/path/to/page",
"http://example.com/path/to/page.html",
"http://example.com/path/to/page.html#title",
"http://example.com/path/to/page.html#title%20here",
"http://example.com/path/to/page.html#title%20here",
"http://example.com/path/to/page?foo=bar&bar=baz",
"http://example.com/path/to/page.html?foo=bar&bar=baz",
"http://example.com/path/to/page.html#title?foo=bar&bar=baz",
"http://user:password@example.com/",
"http://www.example.com/",
"http://www.www.example.com/",
"http://www.www.www.example.com/",
"https://example.com",
"https://www.example.com/",
]
invalid = [
"hppt://example.com/",
"sftp://example.com/",
"http:/example.com/",
"http://www example com/",
"http://www\texample\tcom/",
]
for test in valid:
assert allMatches(regEx, f"Text {test} more text") == [[(test, 5, 5 + len(test))]]
for test in invalid:
assert allMatches(regEx, f"Text {test} more text") == []
@pytest.mark.core
def testTextPatterns_Words():
"""Test the word split regex."""
@@ -198,8 +240,10 @@ def testTextPatterns_ShortcodesPlain():
assert allMatches(regEx, "one [x]two[/x] three") == []
# Line Break Substitution
# =======================
@pytest.mark.core
def testTextPatterns_LineBreakReplace():
"""Test replacing forced line breaks."""
regEx = REGEX_PATTERNS.lineBreak
assert regEx.sub("\n", "one[br]two") == "one\ntwo"