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] +) * $’

re.IGNORECASE)

 

# 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 = regex_name.search (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 

Shop

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

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically