小编use*_*141的帖子

在 Python 中使用正则表达式匹配输入

我有一个程序需要输入“是”,例如:

my_input = raw_input('> ')
if my_input == 'yes':
    #etc
Run Code Online (Sandbox Code Playgroud)

但这太具体了,我希望输入与此正则表达式匹配:[yY](es)?,以便如果用户输入“是,是,y 或 Y”,则是相同的。但我不知道这是如何在 python 中实现的。

我想要这样的东西:

regex = some.regex.method('[yY](es)?')
my_input = raw_input('> ')
if my_input == regex:
    #etc  
Run Code Online (Sandbox Code Playgroud)

先感谢您。

python regex input match

2
推荐指数
1
解决办法
2754
查看次数

标签 统计

input ×1

match ×1

python ×1

regex ×1