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

Python RegEx Compile – re.compile()

# Module Regular Expression is imported
import re

# compile() creates regular expression character class [a-d], which is equivalent to [abcd].
# class [abcd] will match with string with 'a', 'b', 'c', 'd'.
p = re.compile('[a-e]')

# findall() searches for the Regular Expression nd return a list upon finding
print(p.findall("Hello, Welcome to Softhunt.net Tutorial Website"))
Comment

PREVIOUS NEXT
Code Example
Python :: Python JSON API example 
Python :: how to make a button in python turtle 
Python :: python getattr 
Python :: how to get confusion matrix in python 
Python :: python invert binary tree 
Python :: how to put a image in flask 
Python :: suppress python vs try/except pass 
Python :: how do i limit decimals to only two decimals in python 
Python :: int to ascii python 
Python :: how to declare a variable in python 
Python :: python start with 
Python :: python generator 
Python :: requests save data to disk 
Python :: Return the number of times that the string "hi" appears anywhere in the given string. python 
Python :: legend font size python matplotlib 
Python :: code for python shell 3.8.5 games 
Python :: python opencv subtract two images 
Python :: how to run same function on multiple threads in pyhton 
Python :: join dataframe pandas by column 
Python :: how to install pyinstaller 
Python :: django tempalte tag datetime to timestamp 
Python :: python sort dictionary by value 
Python :: python align label left 
Python :: data normalization python 
Python :: hstack 
Python :: how to take input for list in one line in python 
Python :: how to add new column in csv file using pandas 
Python :: how to add a fuction in python 
Python :: how to play and stop music python 
Python :: def extract_title(input_df): 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =