如何匹配确切的用户输入

时间:2015-11-26 10:28:59

标签: python

我在test.py文件中有以下代码:

#!/usr/bin/python
import sys,re

def disfun():
        a = str(raw_input('Enter your choice [PRIMARY|SECONDARY]: ')).upper().strip()
        p = re.compile(r'\s.+a+.\s')
        result = p.findall("+a+")
        print result
disfun()

当我运行此代码时,它会提示输入您的选择,如果我选择为PRIMARY。我只得到空白输出:

[oracle@localhost oracle]$ ./test.py Enter your choice [PRIMARY|SECONDARY]: PRIMARY []

这里我希望获得与用户输入中给出的输出相同的输出。请帮帮我。

1 个答案:

答案 0 :(得分:0)

不确定你的目标是什么,但我认为你的意思是?注意引号,'a'是您的用户输入。如果你写“+ a +”,那么它就是字符串'+ a +'

def disfun():
        a = str(raw_input('Enter your choice [PRIMARY|SECONDARY]: ')).upper().strip()
        p = re.compile(r'.*'+a+'.*') <-- this will match anything containing your user input
        result = p.findall(a)
        print result
相关问题