Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

re.compile example

import re

# Target String one
str1 = "Emma's luck numbers are 251 761 231 451"

# pattern to find three consecutive digits
string_pattern = r"d{3}"
# compile string pattern to re.Pattern object
regex_pattern = re.compile(string_pattern)

# print the type of compiled pattern
print(type(regex_pattern))
# Output <class 're.Pattern'>

# find all the matches in string one
result = regex_pattern.findall(str1)
print(result)
# Output ['251', '761', '231', '451']

# Target String two
str2 = "Kelly's luck numbers are 111 212 415"
# find all the matches in second string by reusing the same pattern
result = regex_pattern.findall(str2)
print(result)
# Output ['111', '212', '415']
Comment

python re compile

import re
#	Compile a regular expression pattern into a regular expression object, which can be used for matching using its match(), search() and other methods, described below.

prog = re.compile(pattern)
result = prog.match(string)

#	is equivalent to

result = re.match(pattern, string)
Comment

PREVIOUS NEXT
Code Example
Python :: poetry python download windows 
Python :: python null 
Python :: import antigravity in python 
Python :: deleting models with sqlalchemy orm 
Python :: python edit global variable in function 
Python :: django collectstatic 
Python :: if list item in string python 
Python :: how to install api in python 
Python :: python face recognition 
Python :: python character list to string 
Python :: django queryset first element 
Python :: suppress python vs try/except 
Python :: make a gif with images python 
Python :: start django project in windows 
Python :: python create venv 
Python :: python dictionary sort 
Python :: mongodb aggregate group 
Python :: finding path of a module in python 
Python :: strp datetime 
Python :: bucketizer pyspark 
Python :: check regex in python 
Python :: turn list in to word python 
Python :: create qr code in python 
Python :: install aws sdk python 
Python :: play video in colab 
Python :: how to reverse a string in python 
Python :: python draw rectangle on image 
Python :: how to install ffmpeg python heroku 
Python :: python dictionary delete by value 
Python :: remove space characters from string in python 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =