如何在python中获得作为动态输入的重模式?

如何在python中获得作为动态输入的重模式?,python,Python,我在python中使用re模块进行一些正则表达式操作。当我在python程序中定义了静态匹配的模式时,它工作得很好 比如说, import re s="hi can you help me out" pattern=r'[a-z ]*' #pattern that takes space and lower case letters only i= re.fullmatch(pattern,s) #to check the entire string print(i.string) outp

我在python中使用re模块进行一些正则表达式操作。当我在python程序中定义了静态匹配的模式时,它工作得很好

比如说,

import re
s="hi can you help me out"
pattern=r'[a-z ]*' #pattern that takes space and lower case letters only
i= re.fullmatch(pattern,s) #to check the entire string
print(i.string)


output:
hi can you help me out
现在让我来谈谈我所面临的问题,如果我试图在运行时从用户那里获取输入模式,它会抛出异常

import re
s="hi can you help me out"
pattern=input("Enter pattern:")
i= re.fullmatch(pattern,s)
print(i.string)

output:
Enter pattern:r'[a-z]*'
Exception has occurred: AttributeError
'NoneType' object has no attribute 'string'
希望有人能帮我解决这个问题

python版本:3.5


提前感谢

您只需输入此部分
[a-z]*
,不带
r
字符串前缀:

Python 3.x

import re
s = "hi can you help me out"
pattern = input("Enter pattern:")
i = re.fullmatch(pattern,s)
print(i.string)
import re
s = "hi can you help me out"
pattern = raw_input("Enter pattern:")   # since input() in python2.x is the same eval(raw_input()) which would return an int
i = re.fullmatch(pattern,s)
print(i.string)
Enter pattern:[a-z ]*
hi can you help me out
Python 2.x

import re
s = "hi can you help me out"
pattern = input("Enter pattern:")
i = re.fullmatch(pattern,s)
print(i.string)
import re
s = "hi can you help me out"
pattern = raw_input("Enter pattern:")   # since input() in python2.x is the same eval(raw_input()) which would return an int
i = re.fullmatch(pattern,s)
print(i.string)
Enter pattern:[a-z ]*
hi can you help me out
输出

import re
s = "hi can you help me out"
pattern = input("Enter pattern:")
i = re.fullmatch(pattern,s)
print(i.string)
import re
s = "hi can you help me out"
pattern = raw_input("Enter pattern:")   # since input() in python2.x is the same eval(raw_input()) which would return an int
i = re.fullmatch(pattern,s)
print(i.string)
Enter pattern:[a-z ]*
hi can you help me out

你参加什么?适用于
Enter模式:[a-z]*
PS。请注意
r'
开头
r'
和结尾
'
不是模式的一部分,因为您输入了
r'[a-z]*
作为输入,而不是
[a-z]*
@GomathiMeena您是否正在使用Python 2.x?@GomathiMeena显示错误