0% found this document useful (0 votes)
2 views23 pages

SRC 7

The document consists of multiple Python scripts that validate email addresses and format names, as well as extract Twitter usernames from URLs. Each script builds upon the previous one, adding complexity and functionality, such as using regular expressions for validation and formatting. The scripts demonstrate various techniques for string manipulation and validation in Python.

Uploaded by

Raheel Hashmi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views23 pages

SRC 7

The document consists of multiple Python scripts that validate email addresses and format names, as well as extract Twitter usernames from URLs. Each script builds upon the previous one, adding complexity and functionality, such as using regular expressions for validation and formatting. The scripts demonstrate various techniques for string manipulation and validation in Python.

Uploaded by

Raheel Hashmi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

validate0.

py

1 # Validates email address by checking for @


2
3 email = input("What's your email? ").strip()
4
5 if "@" in email:
6 print("Valid")
7 else:
8 print("Invalid")
[Link]

1 # Validates email address by checking for . too


2
3 email = input("What's your email? ").strip()
4
5 if "@" in email and "." in email:
6 print("Valid")
7 else:
8 print("Invalid")
[Link]

1 # Validates email address by checking username and domain separately


2
3 email = input("What's your email? ").strip()
4
5 username, domain = [Link]("@")
6
7 if username and "." in domain:
8 print("Valid")
9 else:
10 print("Invalid")
[Link]

1 # Validates email address by checking whether domain ends with .edu


2
3 email = input("What's your email? ").strip()
4
5 username, domain = [Link]("@")
6
7 if username and [Link](".edu"):
8 print("Valid")
9 else:
10 print("Invalid")
[Link]

1 # Validates email address by checking for @ with regex


2
3 import re
4
5 email = input("What's your email? ").strip()
6
7 if [Link]("@", email):
8 print("Valid")
9 else:
10 print("Invalid")
[Link]

1 # Adds .*
2
3 import re
4
5 email = input("What's your email? ").strip()
6
7 if [Link](".*@.*", email):
8 print("Valid")
9 else:
10 print("Invalid")
[Link]

1 # Changes * to +
2
3 import re
4
5 email = input("What's your email? ").strip()
6
7 if [Link](".+@.+", email):
8 print("Valid")
9 else:
10 print("Invalid")
[Link]

1 # Adds \.edu
2
3 import re
4
5 email = input("What's your email? ").strip()
6
7 if [Link](r".+@.+\.edu", email):
8 print("Valid")
9 else:
10 print("Invalid")
[Link]

1 # Adds ^ and $ to regex


2
3 import re
4
5 email = input("What's your email? ").strip()
6
7 if [Link](r"^.+@.+\.edu$", email):
8 print("Valid")
9 else:
10 print("Invalid")
[Link]

1 # Adds character class


2
3 import re
4
5 email = input("What's your email? ").strip()
6
7 if [Link](r"^[a-zA-Z0-9_]+@[a-zA-Z0-9_]+\.edu$", email):
8 print("Valid")
9 else:
10 print("Invalid")
[Link]

1 # Replaces character class with \w


2
3 import re
4
5 email = input("What's your email? ").strip()
6
7 if [Link](r"^\w+@\w+\.edu$", email):
8 print("Valid")
9 else:
10 print("Invalid")
[Link]

1 # Adds [Link]
2
3 import re
4
5 email = input("What's your email? ").strip()
6
7 if [Link](r"^\w+@\w+\.edu$", email, [Link]):
8 print("Valid")
9 else:
10 print("Invalid")
[Link]

1 # Adds optional subdomain


2
3 import re
4
5 email = input("What's your email? ").strip()
6
7 if [Link](r"^\w+@(\w+\.)?\w+\.edu$", email, [Link]):
8 print("Valid")
9 else:
10 print("Invalid")
[Link]

1 # Reformats "last, first" as "first last"


2
3 name = input("What's your name? ").strip()
4 if "," in name:
5 last, first = [Link](", ")
6 name = f"{first} {last}"
7 print(f"hello, {name}")
[Link]

1 # Uses [Link]
2
3 import re
4
5 name = input("What's your name? ").strip()
6 matches = [Link](r"^(.+), (.+)$", name)
7 if matches:
8 last, first = [Link]()
9 name = first + " " + last
10 print(f"hello, {name}")
[Link]

1 # Uses .group
2
3 import re
4
5 name = input("What's your name? ").strip()
6 matches = [Link](r"^(.+), (.+)$", name)
7 if matches:
8 name = [Link](2) + " " + [Link](1)
9 print(f"hello, {name}")
[Link]

1 # Uses walrus operator


2
3 import re
4
5 name = input("What's your name? ").strip()
6 if matches := [Link](r"^(.+), (.+)$", name):
7 name = [Link](2) + " " + [Link](1)
8 print(f"hello, {name}")
[Link]

1 # Extracts Twitter username from URL using [Link]


2
3 url = input("URL: ").strip()
4
5 username = [Link]("[Link] "")
6 print(f"Username: {username}")
[Link]

1 # Extracts Twitter username from URL using [Link]


2
3 url = input("URL: ").strip()
4
5 username = [Link]("[Link]
6 print(f"Username: {username}")
[Link]

1 # Uses [Link]
2
3 import re
4
5 url = input("URL: ").strip()
6
7 username = [Link](r"^[Link] "", url)
8 print(f"Username: {username}")
[Link]

1 # Allows for http, no protocol, and www.


2
3 import re
4
5 url = input("URL: ").strip()
6
7 username = [Link](r"^(https?://)?(www\.)?twitter\.com/", "", url)
8 print(f"Username: {username}")
[Link]

1 # Uses capture group


2
3 import re
4
5 url = input("URL: ").strip()
6
7 matches = [Link](r"^https?://(?:www\.)?twitter\.com/(.+)$", url, [Link])
8 if matches:
9 print("Username:", [Link](1))
[Link]

1 # Ignores query and fragment


2
3 import re
4
5 url = input("URL: ").strip()
6
7 matches = [Link](r"^https?://(?:www\.)?twitter\.com/([a-z0-9_]+)", url, [Link])
8 if matches:
9 print("Username:", [Link](1))

You might also like