-
Notifications
You must be signed in to change notification settings - Fork 0
/
similarProgram.py
61 lines (40 loc) · 1.97 KB
/
similarProgram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#! python3
# phoneAndEmail.py - Finds phone numbers and email addresses on the clipboard.
import pyperclip, re
# -------------------- Find website URLs that begin with http:// or https://. ---------------------
urlText = '''The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
the regular expression composed & commented
could be easily tweaked for RFC compliance,
it was expressly modified to fit & satisfy
these test for an URL shortener:
http://mathiasbynens.be/demo/url-regex
Notes on possible differences from a standard/generic validation:
- utf-8 char class take in consideration the full Unicode range
- TLDs have been made mandatory so single names like "localhost" fails
- protocols have been restricted to ftp, http and https only as requested
Changes:
- IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255
first and last IP address of each class is considered invalid
(since they are broadcast/network addresses)
- Added exclusion of private, reserved and/or local networks ranges
- Made starting path slash optional (http://example.com?foo=bar)
- Allow a dot (.) at the end of hostnames (http://example.com.)
Compressed one-line versions:'''
urlRegex = re.compile(r'''
http[s]? # for http or https
:// # for ://
(?:[a-zA-Z]|[0-9]|[$-_@.&+]|(?:%[0-9a-fA-F][0-9a-fA-F]))+
''', re.VERBOSE)
urls = urlRegex.findall(urlText)
for i in range(len(urls)):
urls[i] = urls[i].rstrip(')')
print(urls)