Signup/Sign In
LAST UPDATED: AUGUST 26, 2021

Check if String Contain Only Defined Characters using Regex

A character set contains a huge number of characters like letters, special characters, numbers, and so on. But we do not need all the character set every time. Sometimes the user needs to only enter some specified set of characters.

We can check that the string contains only the specified set of strings using python Regex.

Python Regex also called REs or regular expressions is a module using which we can specify rules to set the possible strings to match. It is available in re module.

Look at the examples to understand the input and output format.

Input: 'ABCDE'

Output: valid input

Input: '12ABCda'

Output: invalid input

We can check that String contains only defined Characters using multiple methods.

  • re.compile(pattern): It compiles the regular expression into a regular expression object. It is used with a combination of match() or search() or other methods for pattern matching.
  • re.search(pattern, string) : This method scans through the string to find the first location where the string matches the specified regular expression. It returns the matching object. If there is no matching string then it returns none.
  • re.match(pattern,string): If zero or more string matches the regular expression then returns the string object else return none.

Algorithm

Step 1: Define the pattern of the string using regular expression.

Step 2: Use the regex method to match the string with the specified pattern

Step 3: Print the Output

Example: Checking specified string with search() method

In this example, we have used compile() method to convert the regular expression to the regular expression object and used the search() method to match the specified input with the pattern.

# import module for regular expression
import re
#Function to match string with regrex
def match(str,pattern):
  if re.search(pattern,str):
    print("valid string")
  else:
    print("invalid string")

#driver code

pattern = re.compile('[A-Z]+$')
match('ABCDE',pattern)
match('12ABCda',pattern)
match('12345',pattern)

Output

Here is the output of the above code.

pattern matching

Example: Checking specified string with match() method

In this example, we have used match() method instead of search() method to check whether the specified string matches the pattern.

# import module for regular expression
import re
#Function to match string with regrex
def match(str,pattern):
  if re.match(pattern,str):
    print("valid string")
  else:
    print("invalid string")

#driver code

pattern = re.compile('[A-Z]+$')
match('ABCDE',pattern)
match('ABCda',pattern)
match('12345',pattern)
match('abcd',pattern)

Output

Here is the output of the above code.

pattern matching

Conclusion

In this tutorial, we have learned to find that the given string contains the specified character or not using python Regex. For this, we have imported re module and used its method like compile(), match(), and search().



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.