Python Regex :
              Regular Expressions (Regex) are patterns used to search, match, and manipulate text.

They are useful for:

  • validation (email, phone numbers, passwords)
  • searching text
  • data cleaning
  • extracting information
  • log & file parsing

Python
import re


Basic Regex Functions :

FunctionDescription
re.search()Searches for first match
re.findall()Returns all matches
re.match()Checks match from start
re.split()Splits string using pattern
re.sub()Replaces matched text


re.search( ) :

Python
import re text = "Python is awesome" result = re.search("awesome", text) print(result) 


re.findall( ) :

Python
import re text = "cat bat mat rat" print(re.findall("at", text)) #output ['at', 'at', 'at', 'at'] 


re.match( ) :

Python
import re text = "Python programming" print(re.match("Python", text)) 


re.split( ) :

Python
import re text = "apple,banana,grapes" print(re.split(",", text)) #output ['apple', 'banana', 'grapes'] 


re.sub( ) :

Python
import re text = "I love Python" print(re.sub("Python", "Java", text)) I love Java 


Regex Meta Characters :

SymbolMeaning
.any character
^start of string
$end of string
*0 or more
+1 or more
?optional
[]character set
{}quantity
()group


Quantifiers :

PatternMeaning
a*0 or more a
a+1 or more a
a?0 or 1 a
a{3}exactly 3 a
a{2,5}between 2 and 5 a