aboutsummaryrefslogtreecommitdiff
path: root/Hotline/Utility/RegularExpressions.swift
diff options
context:
space:
mode:
authorDustin Mierau <dustin@mierau.me>2024-05-05 19:17:39 -0700
committerDustin Mierau <dustin@mierau.me>2024-05-05 19:17:39 -0700
commit3feb709522b38f1aa5ddedc5e1aee28597375aab (patch)
tree9df2ad849703004901261c53bcffad318b876ad4 /Hotline/Utility/RegularExpressions.swift
parent4da73c630183e482e4ab2accef97ad54c6741bf4 (diff)
Write better URL regular expressions for our needs.
Diffstat (limited to 'Hotline/Utility/RegularExpressions.swift')
-rw-r--r--Hotline/Utility/RegularExpressions.swift116
1 files changed, 116 insertions, 0 deletions
diff --git a/Hotline/Utility/RegularExpressions.swift b/Hotline/Utility/RegularExpressions.swift
index 0314dd7..0a0dda9 100644
--- a/Hotline/Utility/RegularExpressions.swift
+++ b/Hotline/Utility/RegularExpressions.swift
@@ -20,4 +20,120 @@ struct RegularExpressions {
}
}
}
+
+ static let supportedLinkScheme = Regex {
+ Anchor.startOfLine
+ ChoiceOf {
+ "hotline"
+ "http"
+ "https"
+ }
+ "://"
+ }.ignoresCase().anchorsMatchLineEndings()
+
+ static let relaxedLink = Regex {
+ ChoiceOf {
+ Anchor.startOfLine
+ Anchor.wordBoundary
+ }
+ Capture {
+ // scheme (optional)
+ Optionally {
+ ChoiceOf {
+ "hotline://"
+ "http://"
+ "https://"
+ }
+ }
+ // domain name
+ OneOrMore {
+ CharacterClass(
+ .anyOf(".-"),
+ ("a"..."z"),
+ ("0"..."9")
+ )
+ }
+ // top-level domain name
+ "."
+ ChoiceOf {
+ "com"
+ "net"
+ "org"
+ "edu"
+ "gov"
+ "mil"
+ "aero"
+ "asia"
+ "biz"
+ "cat"
+ "coop"
+ "info"
+ "int"
+ "jobs"
+ "mobi"
+ "museum"
+ "name"
+ "pizza"
+ "post"
+ "pro"
+ "red"
+ "tel"
+ "today"
+ "travel"
+ "ai"
+ "be"
+ "by"
+ "ca"
+ "co"
+ "de"
+ "er"
+ "es"
+ "fr"
+ "gs"
+ "ie"
+ "im"
+ "in"
+ "io"
+ "is"
+ "it"
+ "jp"
+ "la"
+ "ly"
+ "ma"
+ "md"
+ "me"
+ "my"
+ "nl"
+ "ps"
+ "pt"
+ "ja"
+ "st"
+ "to"
+ "tv"
+ "uk"
+ "ws"
+ }
+ // Port
+ Optionally {
+ ":"
+ OneOrMore {
+ CharacterClass(.digit)
+ }
+ }
+ // path
+ ZeroOrMore {
+ CharacterClass(
+ .anyOf("#_-/.?=&%\\()[]"),
+ ("a"..."z"),
+ ("0"..."9")
+ )
+ }
+ }
+ ChoiceOf {
+ Anchor.endOfLine
+ Anchor.wordBoundary
+ }
+ }
+ .anchorsMatchLineEndings()
+ .ignoresCase()
}