Python program to verify that a string only contains letters, numbers, underscores and dashes
Given a string, we have to find whether the string contains any letters, numbers, underscores, and dashes or not. It is usually used for verifying username and password validity. For example, the user has a string for the username of a person and the user doesn’t want the username to have any special characters such as @, $, etc.
Prerequisite: Regular Expression in Python
Let’s see the different methods for solving this task:
Method 1: Using Regular Expression.
There is a function in the regular expression library(re) that compares two strings for us. re.match(pattern, string) is a function that returns an object, to find whether a match is find or not we have to typecast it into boolean.
Syntax:
re.match(pattern, string)Parameters:
- pattern: the pattern against which you want to check
- string: the string you want to check for the pattern
Return: Match object
Let’s see an example:
Example 1:
Python3
# import library import re # make a pattern pattern = "^[A-Za-z0-9_-]*$" # input string = "G33ks_F0r_Geeks" # convert match object # into boolean values state = bool (re.match(pattern, string)) print (state) |
Output:
True
Example 2:
Python3
# import library import re print ( bool (re.match( "^[A-Za-z0-9_-]*$" , 'ValidString12-_' ))) print ( bool (re.match( "^[A-Za-z0-9_-]*$" , 'inv@lidstring' ))) |
Output:
True False
Method 2: Using Set.
Set is a built-in data type in Python. We are using issubset() function of a set that returns True if all characters of a set are present in a given set Otherwise False.
Syntax:
set_name.issubset(set)Parameters:
- set: Represents that set in which the subset has to be searched
Return: boolean value
Let’s see an example:
Example 1:
Python3
# create a set of allowed characters allowed_chars = set ( ( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-" )) # input string string = "inv@lid" # convert string into set of characters validation = set ((string)) # check condition if validation.issubset(allowed_chars): print ( "True" ) else : print ( "False" ) |
Output:
False
Example 2:
Python3
allowed_chars = set ( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-" ) string = "__Val1d__" validation = set ((string)) if validation.issubset(allowed_chars): print ( "True" ) else : print ( "False" ) |
Output:
True
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Auxiliary Space: O(n)
Method : Using replace() and len() methods
Python3
allowed_chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-" string = "__Val1d__" for i in allowed_chars: string = string.replace(i, "") if len (string) = = 0 : print ( "True" ) else : print ( "False" ) |
True
Method : Using operator.countOf() method
Python3
import operator as op # create a string of allowed characters allowed_chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-" # input string string = "inv@lid" res = True for i in string: if op.countOf(allowed_chars, i) = = 0 : res = False # check condition if res: print ( "True" ) else : print ( "False" ) |
False
Time Complexity: O(N)
Auxiliary Space : O(1)
Method: Using the filter() function with a lambda function.
Algorithm:
- Initialize the allowed_chars string with all the valid characters.
- Initialize the string variable with the input string.
- Use the filter() function with a lambda function to filter out any characters in string that are not in allowed_chars.
- Convert the filtered object to a list and check if its length is equal to the length of the original string.
- If the lengths are equal, output True. Otherwise, output False.
Python3
allowed_chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-" string = "__Val1d__" filtered = filter ( lambda c: c in allowed_chars, string) print ( len ( list (filtered)) = = len (string)) #this code contributed by tvsk |
True
Time complexity:
The time complexity of the filter() function is O(n), where n is the length of the input string. The time complexity of the list() function is also O(n). The time complexity of the comparison operation len(list(filtered)) == len(string) is O(1). Therefore, the overall time complexity of the code is O(n).
Auxiliary space:
The space complexity of the filtered list is O(n), where n is the length of the input string. The space complexity of the list() function is also O(n). Therefore, the overall auxiliary space complexity of the code is O(n).
Please Login to comment...