0% found this document useful (0 votes)
4 views4 pages

Validate User Credentials with Regex

The document outlines an experiment to validate usernames, passwords, and URLs using regular expressions (regex). It explains various metacharacters and special characters used in regex, provides rules for validating 10-digit mobile numbers, and includes a Python program for password validation. The conclusion states that the implementation of regex expressions has been successfully demonstrated.
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)
4 views4 pages

Validate User Credentials with Regex

The document outlines an experiment to validate usernames, passwords, and URLs using regular expressions (regex). It explains various metacharacters and special characters used in regex, provides rules for validating 10-digit mobile numbers, and includes a Python program for password validation. The conclusion states that the implementation of regex expressions has been successfully demonstrated.
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

EXPERIMENT NO: 14

AIM: To validate username, password and url using regular expressions


THEORY: Regular expressions, often shortened as regex are a sequence of characters used to
check whether a pattern exists in a given text/string or not.
METACHARACTERS:
[ ] - The set of characters - “[a-e]”
 - Any character – “he.ϴ”
˄ - Starts with – “^python”
\ - Signals a special sequence
* - Zero or more occurrences - “aix*”
+ - One or more occurrences - “aix+”
{ } - Exactly the specified number of occurrences – “al{2}”
| - Either or – “code|char”

SPECIAL CHARACTERS
\A – Returns a match if the specified characters are at the beginning of the string
\b – Returns a match where the specified characters are at the beginning or at the end of a word
\B – Returns a match where the specified characters are present, but NOT at the beginning or at
end of a word.
PROGRAMS:
Write a Regular Expression to represent all 10 digit mobile numbers
Rules:
1. Every number should contain exactly 10 digits
2. The first digit should be 7 or 8 or 9
password validation:
import re

def validate_password(password):
# define our regex pattern for validation
pattern = r"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$"

# We use the [Link] function to test the password against the pattern
match = [Link](pattern, password)

# return True if the password matches the pattern, False otherwise


return bool(match)

password1 = "StrongP@ssword123"
password2 = "weakpassword"
print(validate_password(password1))
print(validate_password(password2))

CONCLUSION: Thereby, we have implemented regex expression.

Common questions

Powered by AI

This regex pattern ensures password diversity by requiring the inclusion of at least one digit, one lowercase letter, and one uppercase letter, in addition to enforcing a minimum length of 8 characters. Unlike simpler patterns that might only check for length or character type, this pattern enforces the use of diverse character types, thus preventing weak passwords composed only of letters or digits, thereby enhancing security .

The '\b' special character is used for matching positions at the beginning or end of a word within a string. A potential use case includes finding whole words that match a specified pattern without matching those patterns when they occur as substrings within longer words .

The '^' metacharacter is used to specify that a string starts with a particular sequence. For example, '^python' would match any string that begins with 'python', providing a way to anchor patterns to the start of strings .

Regular expressions are sequences of characters used to determine if a pattern exists within a given text or string. They often include metacharacters and special sequences that dictate the pattern to be matched, such as the range of characters specified in brackets '[ ]', zero or more occurrences '*', and one or more occurrences '+' .

The '|' pattern is important because it allows for the specification of alternative matches. In regex validation processes, this enables flexibility by allowing one pattern or another to be valid, which is critical in accommodating variations in input data .

The rules for mobile number validation, which specify that numbers must start with '7', '8', or '9' and be exactly 10 digits long, align with telecommunication practices in regions where these numbers define standard mobile ranges. These criteria ensure compliance with the numbering structures used by telecommunication networks .

The document suggests using the 're.match' function in Python to test passwords against the regex pattern. If the 'match' function does not find a match, it returns 'False', indicating the password does not meet the validation criteria. This binary return can be used to handle validation failures programmatically .

Metacharacters, such as brackets '[ ]' and symbols like '*', define patterns and the structure of matching operations. Special characters, on the other hand, such as '\A' and '\b', specify positional or conditional matches within a string. The main difference lies in the application: metacharacters define general matching conditions, while special characters are more about positional matching .

The regex pattern for matching a 10-digit mobile number ensures valid input by requiring the number to contain exactly 10 digits and start with either '7', '8', or '9'. This is achieved through specific character sequences and constraints defined within the pattern .

The regex pattern for password validation requires the presence of at least one digit '(?=.*\d)', one lowercase letter '(?=.*[a-z])', and one uppercase letter '(?=.*[A-Z])', with the entire string being a minimum of 8 characters long '.{8,}'. This ensures passwords have complexity by including characters of different types and a minimum length for security .

You might also like