SlideShare a Scribd company logo
Common Regex Functions
Introduction to Regular Expressions
What is a Regular Expression?
• A language to specify the rules for the set of possible strings that you want to search in a corpus or text
• A regular expression (regex) is a sequence of characters that form a search pattern.
• Used for string searching and manipulation in programming languages.
Regex Module in Python
• Functions available: match(), search(), findall(), compile(), etc.
Common Functions in Regex
Syntax: re.match(pattern, string)
Matches a pattern only at the beginning of the string.
import re
re.match(r"d+", "123abc") # Matches '123'
• Python’s re module is used to work with regex patterns.
re.match()
re.search()
Syntax: re.search(pattern, string)
Searches for the first location where the pattern matches.
import re
re.search(r"d+", "Order number: 56789")
It returns a match object for 56789
re.findall()
Syntax: re.findall(pattern, string)
Returns all matches of a pattern in the string as a list.
re.findall(r"d+", "abc123def456") # Returns ['123', '456']
re.finditer()
Syntax: re.finditer(pattern, string)
Returns an iterator yielding match objects for all matches.
import re
for match in re.finditer(r"[A-Za-z]+", "123abc456def789ghi"):
print(match.group())
# Outputs 'abc', 'def', and 'ghi'
re.compile()
Syntax: re.compile(pattern)
Compiles a regex pattern for repeated use.
import re
pattern = re.compile(r"Tw+")
text = "Today is Tuesday, and tomorrow is Thursday."
matches = pattern.findall(text)
print(matches) # Outputs: ['Today', 'Tuesday', 'Thursday']
re.sub
Purpose: Replaces occurrences of a pattern in a string with a specified replacement.
import re
text = "I have a cat and a cat."
result = re.sub(r"cat", "dog", text) # Replaces 'cat' with 'dog'
print(result)
Output: "I have a dog and a dog."
re.split
Purpose: Splits a string into a list using a specified pattern as the delimiter.
import re
text = "apple,orange;banana|grape"
result = re.split(r"[;,|]", text) # Splits on commas, semicolons, or pipes
print(result)
Output: ['apple', 'orange', 'banana', 'grape']
• re.IGNORECASE (or re.I): Ignores case during matching.
• re.MULTILINE (or re.M): Treats the string as multiple lines.
• re.DOTALL (or re.S): Allows . to match newline characters
Flags in Regex
Common Flags

More Related Content

PDF
Regular expression in python for students
PDF
regular-expression.pdf
PDF
Python - Lecture 7
DOCX
Python - Regular Expressions
PDF
Regular expressions
PDF
Python (regular expression)
PPTX
unit-4 regular expression.pptx
PPT
Adv. python regular expression by Rj
Regular expression in python for students
regular-expression.pdf
Python - Lecture 7
Python - Regular Expressions
Regular expressions
Python (regular expression)
unit-4 regular expression.pptx
Adv. python regular expression by Rj

Similar to Common regex pp functions wweweewwt.pptx (20)

PPTX
Python- Regular expression
PPTX
Python advanced 2. regular expression in python
PDF
Python regular expressions
PDF
Python : Regular expressions
PPTX
Regular expressions in Python
PPTX
regex.pptx
PDF
Module 3 - Regular Expressions, Dictionaries.pdf
PPTX
Regular Expressions
PPTX
Regular Expressions(Theory of programming languages))
PPTX
Regular expressions,function and glob module.pptx
PPTX
Regular_Expressions.pptx
PPTX
UNIT-4( pythonRegular Expressions) (3).pptx
PPTX
Processing Regex Python
PDF
Python Regular Expressions
PDF
Text Mining using Regular Expressions
PDF
A3 sec -_regular_expressions
ODP
OISF: Regular Expressions (Regex) Overview
ODP
DerbyCon 7.0 Legacy: Regular Expressions (Regex) Overview
PPTX
Pythonlearn-11-Regex.pptx
PPTX
P3 2018 python_regexes
Python- Regular expression
Python advanced 2. regular expression in python
Python regular expressions
Python : Regular expressions
Regular expressions in Python
regex.pptx
Module 3 - Regular Expressions, Dictionaries.pdf
Regular Expressions
Regular Expressions(Theory of programming languages))
Regular expressions,function and glob module.pptx
Regular_Expressions.pptx
UNIT-4( pythonRegular Expressions) (3).pptx
Processing Regex Python
Python Regular Expressions
Text Mining using Regular Expressions
A3 sec -_regular_expressions
OISF: Regular Expressions (Regex) Overview
DerbyCon 7.0 Legacy: Regular Expressions (Regex) Overview
Pythonlearn-11-Regex.pptx
P3 2018 python_regexes
Ad

Recently uploaded (20)

PPTX
PE3-WEEK-3sdsadsadasdadadwadwdsdddddd.pptx
PPTX
AREAS OF SPECIALIZATION AND CAREER OPPORTUNITIES FOR COMMUNICATORS AND JOURNA...
PPTX
退学买新西兰毕业证(WelTec毕业证书)惠灵顿理工学院毕业证国外证书制作
PDF
Daisia Frank: Strategy-Driven Real Estate with Heart.pdf
PPT
BCH3201 (Enzymes and biocatalysis)-JEB (1).ppt
PDF
MCQ Practice CBT OL Official Language 1.pptx.pdf
PDF
L-0018048598visual cloud book for PCa-pdf.pdf
PPTX
Nervous_System_Drugs_PPT.pptxXXXXXXXXXXXXXXXXX
PPTX
microtomy kkk. presenting to cryst in gl
PPTX
ESD MODULE-5hdbdhbdbdbdbbdbdbbdndbdbdbdbbdbd
PPTX
Your Guide to a Winning Interview Aug 2025.
PDF
Biography of Mohammad Anamul Haque Nayan
PPTX
chapter 3_bem.pptxKLJLKJLKJLKJKJKLJKJKJKHJH
PPTX
cse couse aefrfrqewrbqwrgbqgvq2w3vqbvq23rbgw3rnw345
PPT
Gsisgdkddkvdgjsjdvdbdbdbdghjkhgcvvkkfcxxfg
PDF
シュアーイノベーション採用ピッチ資料|Company Introduction & Recruiting Deck
PPTX
internship presentation of bsnl in colllege
PDF
Prostaglandin E2.pdf orthoodontics op kharbanda
PPTX
The Stock at arrangement the stock and product.pptx
PPTX
OnePlus 13R – ⚡ All-Rounder King Performance: Snapdragon 8 Gen 3 – same as iQ...
PE3-WEEK-3sdsadsadasdadadwadwdsdddddd.pptx
AREAS OF SPECIALIZATION AND CAREER OPPORTUNITIES FOR COMMUNICATORS AND JOURNA...
退学买新西兰毕业证(WelTec毕业证书)惠灵顿理工学院毕业证国外证书制作
Daisia Frank: Strategy-Driven Real Estate with Heart.pdf
BCH3201 (Enzymes and biocatalysis)-JEB (1).ppt
MCQ Practice CBT OL Official Language 1.pptx.pdf
L-0018048598visual cloud book for PCa-pdf.pdf
Nervous_System_Drugs_PPT.pptxXXXXXXXXXXXXXXXXX
microtomy kkk. presenting to cryst in gl
ESD MODULE-5hdbdhbdbdbdbbdbdbbdndbdbdbdbbdbd
Your Guide to a Winning Interview Aug 2025.
Biography of Mohammad Anamul Haque Nayan
chapter 3_bem.pptxKLJLKJLKJLKJKJKLJKJKJKHJH
cse couse aefrfrqewrbqwrgbqgvq2w3vqbvq23rbgw3rnw345
Gsisgdkddkvdgjsjdvdbdbdbdghjkhgcvvkkfcxxfg
シュアーイノベーション採用ピッチ資料|Company Introduction & Recruiting Deck
internship presentation of bsnl in colllege
Prostaglandin E2.pdf orthoodontics op kharbanda
The Stock at arrangement the stock and product.pptx
OnePlus 13R – ⚡ All-Rounder King Performance: Snapdragon 8 Gen 3 – same as iQ...
Ad

Common regex pp functions wweweewwt.pptx

  • 2. Introduction to Regular Expressions What is a Regular Expression? • A language to specify the rules for the set of possible strings that you want to search in a corpus or text • A regular expression (regex) is a sequence of characters that form a search pattern. • Used for string searching and manipulation in programming languages.
  • 3. Regex Module in Python • Functions available: match(), search(), findall(), compile(), etc. Common Functions in Regex Syntax: re.match(pattern, string) Matches a pattern only at the beginning of the string. import re re.match(r"d+", "123abc") # Matches '123' • Python’s re module is used to work with regex patterns. re.match()
  • 4. re.search() Syntax: re.search(pattern, string) Searches for the first location where the pattern matches. import re re.search(r"d+", "Order number: 56789") It returns a match object for 56789 re.findall() Syntax: re.findall(pattern, string) Returns all matches of a pattern in the string as a list. re.findall(r"d+", "abc123def456") # Returns ['123', '456']
  • 5. re.finditer() Syntax: re.finditer(pattern, string) Returns an iterator yielding match objects for all matches. import re for match in re.finditer(r"[A-Za-z]+", "123abc456def789ghi"): print(match.group()) # Outputs 'abc', 'def', and 'ghi' re.compile() Syntax: re.compile(pattern) Compiles a regex pattern for repeated use. import re pattern = re.compile(r"Tw+") text = "Today is Tuesday, and tomorrow is Thursday." matches = pattern.findall(text) print(matches) # Outputs: ['Today', 'Tuesday', 'Thursday']
  • 6. re.sub Purpose: Replaces occurrences of a pattern in a string with a specified replacement. import re text = "I have a cat and a cat." result = re.sub(r"cat", "dog", text) # Replaces 'cat' with 'dog' print(result) Output: "I have a dog and a dog." re.split Purpose: Splits a string into a list using a specified pattern as the delimiter. import re text = "apple,orange;banana|grape" result = re.split(r"[;,|]", text) # Splits on commas, semicolons, or pipes print(result) Output: ['apple', 'orange', 'banana', 'grape']
  • 7. • re.IGNORECASE (or re.I): Ignores case during matching. • re.MULTILINE (or re.M): Treats the string as multiple lines. • re.DOTALL (or re.S): Allows . to match newline characters Flags in Regex Common Flags