44from botcore .utils .regex import DISCORD_INVITE
55
66
7- def use_regex (s : str ) -> Optional [str ]:
8- """Helper function to run the Regex on a string.
7+ def match_regex (s : str ) -> Optional [str ]:
8+ """Helper function to run re.match on a string.
9+
10+ Return the invite capture group, if the string matches the pattern
11+ else return None
12+ """
13+ result = DISCORD_INVITE .match (s )
14+ return result if result is None else result .group ("invite" )
15+
16+
17+ def search_regex (s : str ) -> Optional [str ]:
18+ """Helper function to run re.search on a string.
919
1020 Return the invite capture group, if the string matches the pattern
1121 else return None
@@ -19,32 +29,37 @@ class UtilsRegexTests(unittest.TestCase):
1929 def test_discord_invite_positives (self ):
2030 """Test the DISCORD_INVITE regex on a set of strings we would expect to capture."""
2131
22- self .assertEqual (use_regex ("discord.gg/python" ), "python" )
23- self .assertEqual (use_regex ("https://discord.gg/python" ), "python" )
24- self .assertEqual (use_regex ("discord.com/invite/python" ), "python" )
25- self .assertEqual (use_regex ("discordapp.com/invite/python" ), "python" )
26- self .assertEqual (use_regex ("discord.me/python" ), "python" )
27- self .assertEqual (use_regex ("discord.li/python" ), "python" )
28- self .assertEqual (use_regex ("discord.io/python" ), "python" )
29- self .assertEqual (use_regex (".gg/python" ), "python" )
30-
31- self .assertEqual (use_regex ("discord.gg/python/but/extra" ), "python/but/extra" )
32- self .assertEqual (use_regex ("discord.me/this/isnt/python" ), "this/isnt/python" )
33- self .assertEqual (use_regex (".gg/a/a/a/a/a/a/a/a/a/a/a" ), "a/a/a/a/a/a/a/a/a/a/a" )
34- self .assertEqual (use_regex ("discordapp.com/invite/python/snakescord" ), "python/snakescord" )
35- self .assertEqual (use_regex ("http://discord.gg/python/%20/notpython" ), "python/%20/notpython" )
36- self .assertEqual (use_regex ("discord.gg/python?=ts/notpython" ), "python?=ts/notpython" )
37- self .assertEqual (use_regex ("https://discord.gg/python#fragment/notpython" ), "python#fragment/notpython" )
38- self .assertEqual (use_regex ("https://discord.gg/python/~/notpython" ), "python/~/notpython" )
39-
40- self .assertEqual (use_regex ("https://discord.gg/python with whitespace" ), "python" )
41- self .assertEqual (use_regex (" https://discord.gg/python " ), "python" )
32+ self .assertEqual (match_regex ("discord.gg/python" ), "python" )
33+ self .assertEqual (match_regex ("https://discord.gg/python" ), "python" )
34+ self .assertEqual (match_regex ("https://www.discord.gg/python" ), "python" )
35+ self .assertEqual (match_regex ("discord.com/invite/python" ), "python" )
36+ self .assertEqual (match_regex ("www.discord.com/invite/python" ), "python" )
37+ self .assertEqual (match_regex ("discordapp.com/invite/python" ), "python" )
38+ self .assertEqual (match_regex ("discord.me/python" ), "python" )
39+ self .assertEqual (match_regex ("discord.li/python" ), "python" )
40+ self .assertEqual (match_regex ("discord.io/python" ), "python" )
41+ self .assertEqual (match_regex (".gg/python" ), "python" )
42+
43+ self .assertEqual (match_regex ("discord.gg/python/but/extra" ), "python/but/extra" )
44+ self .assertEqual (match_regex ("discord.me/this/isnt/python" ), "this/isnt/python" )
45+ self .assertEqual (match_regex (".gg/a/a/a/a/a/a/a/a/a/a/a" ), "a/a/a/a/a/a/a/a/a/a/a" )
46+ self .assertEqual (match_regex ("discordapp.com/invite/python/snakescord" ), "python/snakescord" )
47+ self .assertEqual (match_regex ("http://discord.gg/python/%20/notpython" ), "python/%20/notpython" )
48+ self .assertEqual (match_regex ("discord.gg/python?=ts/notpython" ), "python?=ts/notpython" )
49+ self .assertEqual (match_regex ("https://discord.gg/python#fragment/notpython" ), "python#fragment/notpython" )
50+ self .assertEqual (match_regex ("https://discord.gg/python/~/notpython" ), "python/~/notpython" )
51+
52+ self .assertEqual (search_regex ("https://discord.gg/python with whitespace" ), "python" )
53+ self .assertEqual (search_regex (" https://discord.gg/python " ), "python" )
4254
4355 def test_discord_invite_negatives (self ):
4456 """Test the DISCORD_INVITE regex on a set of strings we would expect to not capture."""
4557
46- self .assertEqual (use_regex ("another string" ), None )
47- self .assertEqual (use_regex ("https://pythondiscord.com" ), None )
48- self .assertEqual (use_regex ("https://discord.com" ), None )
49- self .assertEqual (use_regex ("https://discord.gg" ), None )
50- self .assertEqual (use_regex ("https://discord.gg/ python" ), None )
58+ self .assertEqual (match_regex ("another string" ), None )
59+ self .assertEqual (match_regex ("https://pythondiscord.com" ), None )
60+ self .assertEqual (match_regex ("https://discord.com" ), None )
61+ self .assertEqual (match_regex ("https://discord.gg" ), None )
62+ self .assertEqual (match_regex ("https://discord.gg/ python" ), None )
63+
64+ self .assertEqual (search_regex ("https://discord.com with whitespace" ), None )
65+ self .assertEqual (search_regex (" https://discord.com " ), None )
0 commit comments