Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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_Regex - Fatal编程技术网

在python中使用正则表达式检查表达式是否有效

在python中使用正则表达式检查表达式是否有效,python,regex,Python,Regex,我试图检查字符串是否是有效的数学表达式。允许的操作有+、-、*、/和^。我试过这个,不知道为什么不起作用: a = raw_input("Unesite izraz") if len( re.findall(r'(\d+(?:.\d+)?(?: [\+\-\/\*\^]\d+(?:.\d+) )* )', a ) ) != 0: 但是这个正则表达式为有效表达式返回[]。为什么?谢谢 在错误的位置有空格 # yours / fixed r'(\d+(?:.\d+)?(?: [\+\-\/\*\^

我试图检查字符串是否是有效的数学表达式。允许的操作有+、-、*、/和^。我试过这个,不知道为什么不起作用:

a = raw_input("Unesite izraz")
if len( re.findall(r'(\d+(?:.\d+)?(?: [\+\-\/\*\^]\d+(?:.\d+) )* )', a ) ) != 0:

但是这个正则表达式为有效表达式返回[]。为什么?谢谢

在错误的位置有空格

# yours / fixed
r'(\d+(?:.\d+)?(?: [\+\-\/\*\^]\d+(?:.\d+) )* )'
r'(\d+(?:.\d+)?(?: [\+\-\/\*\^] \d+(?:.\d+) )*)'

您可以在

尝试,因为您在错误的位置有空格

# yours / fixed
r'(\d+(?:.\d+)?(?: [\+\-\/\*\^]\d+(?:.\d+) )* )'
r'(\d+(?:.\d+)?(?: [\+\-\/\*\^] \d+(?:.\d+) )*)'

您可以在

上试用它们。您可以简化您的regexp

有运算符“除外”:
[^abc]
因此,它将接受任何不是字符“a”、“b”或“c”的内容


您可以使用此网站学习和测试您的regexp:

您可以简化您的regexp

有运算符“除外”:
[^abc]
因此,它将接受任何不是字符“a”、“b”或“c”的内容


您可以使用此网站学习和测试您的regexp:

简单符号数学表达式的验证器可以是这样的东西。
这将是整个字符串的1次匹配

^\s*[+-]?\s*(?:\d+(?:\.\d*)?\.\d+(:\s*[-+/*^]\s*\s*[+-]?\s*(?:\d+(?:\.\d*)?\.\d+)*\s*$

格式:

 ^                             # BOS
 \s* [+-]? \s*                 # whitespace (opt), sign (opt), whitespace (opt)
 (?:                           # Integer or decimal
      \d+ 
      (?: \. \d* )?
   |  \. \d+ 
 )
 (?:                           # Cluster group
      \s* [-+/*^] \s*               # whitespace (opt), operation symbol (req'd), whitespace (opt)
      \s* [+-]? \s*                 # whitespace (opt), sign (opt), whitespace (opt)
      (?:                           # Integer or decimal
           \d+ 
           (?: \. \d* )?
        |  \. \d+ 
      )
 )*                            # End cluster, do 0 to many times
 \s*                           # optional whitespace
 $                             # EOS

简单符号数学表达式的验证器可以如下所示。
这将是整个字符串的1次匹配

^\s*[+-]?\s*(?:\d+(?:\.\d*)?\.\d+(:\s*[-+/*^]\s*\s*[+-]?\s*(?:\d+(?:\.\d*)?\.\d+)*\s*$

格式:

 ^                             # BOS
 \s* [+-]? \s*                 # whitespace (opt), sign (opt), whitespace (opt)
 (?:                           # Integer or decimal
      \d+ 
      (?: \. \d* )?
   |  \. \d+ 
 )
 (?:                           # Cluster group
      \s* [-+/*^] \s*               # whitespace (opt), operation symbol (req'd), whitespace (opt)
      \s* [+-]? \s*                 # whitespace (opt), sign (opt), whitespace (opt)
      (?:                           # Integer or decimal
           \d+ 
           (?: \. \d* )?
        |  \. \d+ 
      )
 )*                            # End cluster, do 0 to many times
 \s*                           # optional whitespace
 $                             # EOS

不是真正的答案,但当您匹配一个数字
\d+(?:。\d+)
时,该点应转义为
\.
,否则
'1a3'
'9(10'
将被视为有效数字。不是真正的答案,而是当您匹配一个数字
\d+(?:。\d+)时?
,点应转义为
\。
,否则
'1a3'
'9(10’
将被视为有效数字。这很有效,谢谢,但我们不应该在+,-等前面使用。
+
在字符类中,它不是量词。。无需转义。
*
^
。这很有效,谢谢,但我们不应该在+,-等前面使用。
+
在c中character类,其中它不是量词。无需转义它。同样地,
*
^