Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Python - Regex Function

Back


Constructor

re.compile(pattern, flags=0)

import re  # Importing re module
print("\n-------- re.compile(pattern, flags=0) --------\n")


# Defining regEx patterns
pattern = "amazing"
regex_object = re.compile(pattern)  # Createing a regEx object
text = "This tutorial is amazing!"   # String

# Searching for the pattern in the string
match_object = regex_object.search(text)
# Match Object: <re.Match object; span=(17, 24), match='amazing'>
print("Match Object:", match_object)

re.match(pattern, string, flags=0)

import re  # Importing re module
print("\n-------- re.match(pattern, string, flags=0) --------\n")

print(re.match(r".?o", "dog"))  # <re.Match object; span=(0, 2), match='do'>
print(re.match("d", "dog"))     # <re.Match object; span=(0, 1), match='d'>
print(re.match("o", "dog"))     # None match='o'>


re.fullmatch(pattern, string, flags=0)

import re  # Importing re module
print("\n-------- re.fullmatch(pattern, string, flags=0) --------\n")

# Sample string
line = "Hello world"

# Using re.fullmatch()
print(re.fullmatch("Hello", line))      # None
print(re.fullmatch("Hello world", line))    # <re.Match object; span=(0, 11), match='Hello world'>

re.search(pattern, string, flags=0): Return 1st

import re  # Importing re module
print("\n-------- re.search(pattern, string, flags=0) --------\n")

print(re.search(r".?o", "dog"))  # <re.Match object; span=(0, 2), match='do'>
print(re.search("d", "dog"))     # <re.Match object; span=(0, 1), match='d'>
print(re.search("o", "dog"))     # <re.Match object; span=(1, 2), match='o'>

Replace

re.sub(pattern, repl, string, count=0, flags=0)

import re  # Importing re module
print("\n-------- re.sub(pattern, repl, string, count=0, flags=0) --------\n")

print(re.sub("\s", "9", "The rain in Spain"))
# The9rain9in9Spain

print(re.sub("\s", "9", "The rain in Spain", 2))
# The9rain9in Spain

re.subn(pattern, repl, string, count=0, flags=0)

import re  # Importing re module
print("\n-------- re.subn(pattern, repl, string, count=0, flags=0) --------\n")

print(re.subn("\s", "9", "The rain in Spain"))
# ('The9rain9in9Spain', 3)

print(re.subn("\s", "9", "The rain in Spain", 2))
# ('The9rain9in Spain', 2)

Other

re.escape(pattern)

import re  # Importing re module
print("\n-------- re.escape(pattern) --------\n")

print(re.escape('https://www.javatpoint.com/'))     # https://www\.javatpoint\.com/

re.purge()

import re  # Importing re module
print("\n-------- re.purge() --------\n")

# Define some regular expressions
pattern1 = r'\d+'
pattern2 = r'[a-z]+'

# Use the regular expressions
# <re.Match object; span=(0, 3), match='123'>
print(re.search(pattern1, '123abc'))
# <re.Match object; span=(3, 6), match='abc'>
print(re.search(pattern2, '123abc'))

# Clear the regular expression cache
re.purge()

# Use the regular expressions again
# <re.Match object; span=(0, 3), match='456'>
print(re.search(pattern1, '456def'))
# <re.Match object; span=(3, 6), match='def'>
print(re.search(pattern2, '456def'))

To List

re.findall(pattern, string, flags=0)


print(re.findall(r'',"which foot or hand fell fastest"))
# ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
print(re.findall(r'o',"which foot or hand fell fastest"))
# ['o', 'o', 'o']

print(re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest'))
# ['foot', 'fell', 'fastest']

print(re.findall(r'(\w+)=(\d+)', 'set width=20 and height=10'))
# [('width', '20'), ('height', '10')]

re.split(pattern, string, maxsplit=0, flags=0)

import re  # Importing re module
print("\n-------- re.split(pattern, string, maxsplit=0, flags=0) --------\n")

print(re.split("\s", "The rain in Spain"))
# ['The', 'rain', 'in', 'Spain']

print(re.split("\s", "The rain in Spain", maxsplit=1))
# ['The', 'rain in Spain']

print(re.split("\s", "The rain in Spain", maxsplit=2))
# ['The', 'rain', 'in Spain']

print(re.split("\s", "The rain in Spain", maxsplit=3))
# ['The', 'rain', 'in', 'Spain']

print(re.split("\s", "The rain in Spain", maxsplit=4))
# ['The', 'rain', 'in', 'Spain']

re.finditer(pattern, string, flags=0)

import re  # Importing re module
print("\n-------- re.finditer(pattern, string, flags=0) --------\n")

# Using re.finditer()
iter_ = re.finditer(r'[aeiou]', "Hello world. I am Here!")

# Iterating the itre_
for i in iter_:
    print(i)

# <re.Match object; span=(1, 2), match='e'>
# <re.Match object; span=(4, 5), match='o'>
# <re.Match object; span=(7, 8), match='o'>
# <re.Match object; span=(15, 16), match='a'>
# <re.Match object; span=(19, 20), match='e'>
# <re.Match object; span=(21, 22), match='e'>

TOP