Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何匹配精确的用户输入_Python - Fatal编程技术网

Python 如何匹配精确的用户输入

Python 如何匹配精确的用户输入,python,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

我在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()
当我运行此代码时,它会提示输入您的选择,如果我将我的选择作为主选项。我只得到空白输出:

[oracle@localhostoracle]$./test.py
输入您的选择[PRIMARY | SECONDARY]:PRIMARY
[]


在这里,我希望得到与用户输入中相同的输出。请在这方面帮助我。

不确定你的目标是什么,但我想你是这个意思吧??注意引号,“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
def disfun():
a=str(原始输入('输入您的选择[PRIMARY | SECONDARY]:')).upper().strip()

p=re.compile(r'.*.+a+'.*')为什么不打印一个呢this@VigneshKalai实际上我将得到的结果,我需要传入一个在不同服务器上运行的命令,并在一个文件中匹配这个结果。你需要更清楚,现在没有任何意义了,谢谢。使用您的代码,我得到的输出为:
['SECONDARY']
,但我只想要SECONDARY,而不是甚至['or']。请告诉我是否有可能我已将“列表”转换为“字符串”,并获得了预期结果。谢谢你的帮助