Change language

Name validation using IGNORECASE in Python Regex

| |

re.IGNORECASE : This flag allows you to match a regular expression to a given string without re.IGNORECASE case, that is, expressions such as [AZ] will also match lowercase letters. Typically, it is passed as an optional argument to re.compile () .

Let’s see an example of a form that asks the user to enter his name and we have to validate it with RegEx. The format for entering the name is as follows:

  • Mister or Mrs. or Miss (Or one) followed by a space
  • First name followed by one space
  • Middle name (optional) followed by one space
  • Last name (optional)

Examples :

  Input : Mr. Albus Severus Potter  Output : Valid  Input : Lily and Mr. Harry Potter  Output : Invalid 

Note. Since we are using the IGNORECASE flag, the first character of the first name, first name and last name may or may not be capitalized.

Below is the Python code —

# Python program to validate name using IGNORECASE in RegEx

# Import re-batch

import re


def validating_name (name):


# RegexObject = re.compile (Regular expression, flag)

# Compiles a regular expression pattern into a regular expression object

  regex_name = re. compile (r ’^ (Mr. | Mrs. | Ms.) ([az] +) ([az] +) * ([az] +) * $’



# RegexObject matches the desired

# string using search function

# If a match is found, search () returns

# MatchObject Instance

  # If no match is found, None is returned

  res = (name)


# If a match is found, the string is valid

if res: print ( "Valid" )


# If no match is found, the string is invalid

  else : print ( " Invalid " )

Driver code

validating_name ( ’ Mr. Albus Severus Potter’ )

validating_name ( ’Lily and Mr. Harry Potter’ )

validating_name ( ’Mr. Cedric’ )

validating_name ( ’Mr. sirius black’ )

Exit :

 Valid Invalid Valid valid 


Learn programming in R: courses


Best Python online courses for 2022


Best laptop for Fortnite


Best laptop for Excel


Best laptop for Solidworks


Best laptop for Roblox


Best computer for crypto mining


Best laptop for Sims 4


Latest questions


psycopg2: insert multiple rows with one query

12 answers


How to convert Nonetype to int or string?

12 answers


How to specify multiple return types using type-hints

12 answers


Javascript Error: IPython is not defined in JupyterLab

12 answers



Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method