如何在 Python 的 if 语句中使用 RegEx?

我正在做类似“语法分析器”的事情Kivy,使用re(正则表达式)。


我只想检查基本操作的有效语法(如 +|-|*|/|(|))。用户将字符串(使用键盘)粘贴起来,然后我使用正则表达式对其进行验证。但我不知道如何在 if 语句中使用正则表达式。我想要的是:如果用户带给我的字符串不正确(或未使用正则表达式检查),则打印“inavlid string”之类的内容,如果正确则打印“Valid string”。


我试过:


if re.match(patron, string) is not None:

    print ("\nTrue")

else:

    print("False")

但是,不管做什么string,应用程序总是显示True.


对不起,我的英语不好。任何帮助将不胜感激!


import  re


patron= re.compile(r"""


    (

    -?\d+[.\d+]?

    [+*-/]

    -?\d+[.\d+]?

    [+|-|*|/]?

    )*

    """, re.X)


obj1= self.ids['text'].text #TextInput

if re.match(patron, obj1) is not None:

    print ("\nValid String")

else:

    print("Inavlid string")

if obj1= "53.22+22.11+10*555+62+55.2-66"实际上它是正确的并且应用程序打印“有效...”但是如果我a像这样输入"a53.22+22.11+10*555+62+55.2-66"它是不正确的并且应用程序必须打印invalid..但它仍然是valid。


守着星空守着你
浏览 136回答 2
2回答

MM们

您的正则表达式始终匹配,因为它允许空字符串匹配(因为整个正则表达式都包含在一个可选组中。如果您在 regex101.com 上对此进行实时测试,您可以立即看到这一点,并且它不匹配整个字符串,而只匹配其中的一部分。我已经更正了字符类中关于使用不必要/有害的交替运算符 ( |) 和不正确放置破折号的两个错误,使其成为范围运算符 ( -),但它仍然不正确。我想你想要更像这样的东西:^&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Make sure the match begins at the start of the string(?:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Start a non-capturing group that matches...&nbsp; &nbsp; -?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # an optional minus sign,&nbsp; &nbsp; \d+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# one or more digits&nbsp; &nbsp; (?:\.\d+)?&nbsp; # an optional group that contains a dot and one or more digits.&nbsp; &nbsp; (?:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Start of a non-capturing group that either matches...&nbsp; &nbsp; &nbsp; &nbsp;[+*/-]&nbsp; &nbsp;# an operator&nbsp; &nbsp; |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# or&nbsp; &nbsp; &nbsp; &nbsp;$&nbsp; &nbsp; &nbsp; &nbsp; # the end of the string.&nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# End of inner non-capturing group)+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # End of outer non-capturing group, required to match at least once.(?<![+*/-])&nbsp; &nbsp; &nbsp;# Make sure that the final character isn't an operator.$&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Make sure that the match ends at the end of the string.

拉丁的传说

这回答了您关于如何将 if 与 regex 一起使用的问题:注意:regex 公式不会清除所有无效输入,例如,两个小数点 ("..")、两个运算符 ("++") 等。所以请调整它以满足您的确切需求)import reregex = re.compile(r"[\d.+\-*\/]+")input_list = [&nbsp; &nbsp; "53.22+22.11+10*555+62+55.2-66", "a53.22+22.11+10*555+62+55.2-66",&nbsp; &nbsp; "53.22+22.pq11+10*555+62+55.2-66", "53.22+22.11+10*555+62+55.2-66zz",]for input_str in input_list:&nbsp; &nbsp; mmm = regex.match(input_str)&nbsp; &nbsp; if mmm and input_str == mmm.group():&nbsp; &nbsp; &nbsp; &nbsp; print('Valid: ', input_str)&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print('Invalid: ', input_str)以上作为用于单个字符串而不是列表的函数:import reregex = re.compile(r"[\d.+\-*\/]+")def check_for_valid_string(in_string=""):&nbsp; &nbsp; mmm = regex.match(in_string)&nbsp; &nbsp; if mmm and in_string == mmm.group():&nbsp; &nbsp; &nbsp; &nbsp; return 'Valid: ', in_string&nbsp; &nbsp; return 'Invalid: ', in_stringcheck_for_valid_string('53.22+22.11+10*555+62+55.2-66')check_for_valid_string('a53.22+22.11+10*555+62+55.2-66')check_for_valid_string('53.22+22.pq11+10*555+62+55.2-66')check_for_valid_string('53.22+22.11+10*555+62+55.2-66zz')输出:## Valid:&nbsp; 53.22+22.11+10*555+62+55.2-66## Invalid:&nbsp; a53.22+22.11+10*555+62+55.2-66## Invalid:&nbsp; 53.22+22.pq11+10*555+62+55.2-66## Invalid:&nbsp; 53.22+22.11+10*555+62+55.2-66zz
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python