RegEx In Python

What is RegEx?

import re
input_user = input("enter your email address: ")
m = re.match( '(?=.*\d)(?=.*[a-z])(?=.*\W)',input_user)
if m:
print("Email is valid:")
else:
print("email is not valid:")

The most common usages of regular expressions are:

  • Search a string (search and match)
  • Finding a string (findall)
  • Break string into substrings (split)
  • Replace part of a string (sub)

‘Re’ Module

import re

Metacharacters

. ^ $ * + ? { } [ ] \ | ( )

Let’s understand what these characters illuminate:

  • [a-e] is the same as [abcde].
  • [1–4] is the same as [1234].
  • [0–9] is the same as [0123 — -9]
  • [^abc] means any character except a or b or c.
  • [⁰-9] means any non-digit character.
Set of Characters in Regular Expressions
s = re.search('\w+$','789Welcome67 to python')

Output:
'python'
s = re.match('........[a-zA-Z0-9]','Welcome to python')

Output:
'Welcome t'

Other Special Sequences

Special Sequences in Regular Expressions

Understanding special sequences with examples

s = re.search('\A\d','789Welcome67 to python')

Output:
'7'
a = re.findall(r'\baa\b', "bbb aa \\bash\baaa")

Output:
['aa']
a = re.findall('[\B]+', "BBB \\Bash BaBe BasketBall")

Output:
['BBB', 'B', 'B', 'B', 'B', 'B']
a = re.match('\d','1Welco+me to python11')

Output:
'1'
a = re.match('\D','Wel12co+me to python11')

Output:
'W'
a = re.match('\s',' Wel12co+me to python11')

Output:
' '
a = re.match('/S','W el12co+me to python11')

Output:
'W'
a = re.match('[\w]','1Welco+me to python11')

Output:
'1'
s = re.match('[\W]','@@Welcome to python')

Output:
'@'
s = re.search('\w\Z','789Welcome67 to python')

Output:
'n'

Module- Level Functions

  1. re.match() — The match() function is used to match the beginning of the string. In the following example, the match() function will match the first letter of the given string whether it is a digit, lowercase or uppercase letter (underscores included).
a = re.match('[0-9_a-zA-Z-]','Welcome to programming')

Output:
'W'
a = re.match('[_0-9A-Za-z-]+','Welcome to programming')

Output:
'Welcome'
a = re.match('[_A-Z0-9a-z-]*','Welcome to programming')

Output:
'Welcome'
a = re.match('[_A-Z0-9a-z-*]','Welcome to programming')

Output:
'W'
a = re.match('[_A-Za-z-]?','Welcome to programming')

Output:
'W'
a = re.search(pattern, string)
a = re.search('come', 'welcome to programming')

Output:
<_sre.SRE_Match object; span=(3, 7), match='come'>
a = re.findall(pattern, string)
a = re.findall('prog','welcome to programming')

Output:
['prog']
a = re.split(pattern, string)
a = re.split('[\W]+','welcome to programming')

Output:
['welcome', 'to', 'programming
a = re.split('\s','Hello how are you')

Output:
['Hello', 'how', 'are', 'you']

Example 3:

b = re.split('\d','hello1i am fine')

Output:
'hello', 'i am fine']
a = re.sub(pattern, replacing value, string)
m = re.sub('[0-9]','','Welcome to python1234. Coding3456.')

Output:
'Welcome to python. Coding.'
name_check = re.compile(r"[^A-Za-zs.]")

name = input("Please, enter your name: ")

while name_check.search(name):
print("please enter your name correctly!")
name = input("Please, enter your name: ")
Please, enter your name: 1234
please enter your name correctly!
Please, enter your name: 5678
Name is provided correctly
please enter your name correctly!
Please, enter your name: john
Name is provided correctly

Wrapping Up

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store