Created
June 5, 2018 22:07
-
-
Save sh78/469385b1cd073c3eda9fde2c5642a891 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
import fileinput | |
import re | |
## | |
# Extract URLs from file or `stdin` | |
# | |
# Prints out a standard list of url address substrings contained in `fileinput` | |
# Call on a file or pipe to `stdin` | |
# | |
# extract-url.py messy_file.txt | |
# ./some-script --messy | extract-url.py | sort | unique > urls.txt | |
## | |
regex = re.compile(r"https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+") | |
string = "" | |
for line in fileinput.input(): | |
string += line | |
matches = re.findall(regex, string) # => array | |
print("\n".join(matches)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment