Created
September 29, 2018 14:29
-
-
Save stringertheory/e8867f7620d8b606c1c675b60c0b173f to your computer and use it in GitHub Desktop.
replacing a python scrubadub regex filter for URL matching that doesn't require protocol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import scrubadub | |
class UrlFilth(scrubadub.filth.url.UrlFilth): | |
regex = re.compile(r''' | |
(?P<protocol> | |
(https?:\/\/(www\.)?|www\.)? # protocol http://, etc | |
)(?P<domain> | |
[\-\w@:%\.\+~\#=]{2,256}\.[a-z]{2,6} # domain name | |
/? # can have a trailing slash | |
)(?P<path> | |
[\-\w@:%\+\.~\#?&/=]* # rest of path, query, & hash | |
) | |
''', re.VERBOSE) | |
class UrlDetector(scrubadub.detectors.base.RegexDetector): | |
filth_cls = UrlFilth | |
SCRUBBER = scrubadub.Scrubber() | |
SCRUBBER.remove_detector('name') | |
SCRUBBER.remove_detector('url') | |
SCRUBBER.add_detector(UrlDetector) | |
SCRUBBER.clean(u''' | |
Link 1: https://example.com | |
Link 2: example.com | |
Email: [email protected] | |
''') | |
# Gives: | |
# Link 1: {{URL}} | |
# Link 2: {{URL}} | |
# Email: {{URL+EMAIL}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment