Home > front end >  Expression regular with python
Expression regular with python

Time:05-11

I am new to regular expressions & Automata theory. I am asked to write a function that compiles and returns the expression pattern object with the pattern string python. I am eager to learn I have tried many ways but I do not even know the output. Is this something related to what have I done. My code below,

import re 
str1 = "Python"
string_pattern = r"\d{3}"
regex_pattern = re.compile(string_pattern)
print(type(regex_pattern))
result = regex_pattern.findall(str1)
print(result)

CodePudding user response:

if your string contains number.

import re 
str1 = "Python123"
string_pattern = r"\d{3}" # this will return the consecutive three number
regex_pattern = re.compile(string_pattern)
print(type(regex_pattern))
result = regex_pattern.findall(str1)
print(result) # ['123']

CodePudding user response:

In order to get a regular expression Pattern object, you can use the re.compile method on a regex string, like the one in your case "Python":

import re

# returns the pattern object to match the string 'Python'
def python_regex():
    return re.compile(r'Python')
  • Related