Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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,我试图用python字符串正则表达式匹配坐标,但没有得到结果。我想看看输入是否是一个有效的坐标定义,但我没有得到正确的匹配使用下面的代码。有人能告诉我怎么了吗 def coordinate(coord): a = re.compile("^(([0-9]+), ([0-9]+))$") b = a.match(coord) if b: return True return False 当前,即使我传入有效坐标的(3,4),它也返回fal

我试图用python字符串正则表达式匹配坐标,但没有得到结果。我想看看输入是否是一个有效的坐标定义,但我没有得到正确的匹配使用下面的代码。有人能告诉我怎么了吗

def coordinate(coord):
     a = re.compile("^(([0-9]+), ([0-9]+))$")
     b = a.match(coord)
     if b:
         return True
     return False

当前,即使我传入有效坐标的
(3,4)
,它也返回false。

您需要将要匹配的括号转义。尝试使用以下方法:

^(\([0-9]+,\s[0-9]+\))$

您需要将要匹配的括号转义。尝试使用以下方法:

^(\([0-9]+,\s[0-9]+\))$

您需要将要匹配的括号转义。尝试使用以下方法:

^(\([0-9]+,\s[0-9]+\))$

您需要将要匹配的括号转义。尝试使用以下方法:

^(\([0-9]+,\s[0-9]+\))$

在我看来,您似乎没有正确地对构成坐标语法的括号进行转义。在正则表达式中,括号是。您需要通过以下方式逃离它们:

>>> a = re.compile("^\(([0-9]+), ([0-9]+)\)$")
>>> a.match("(3, 4)")
<_sre.SRE_Match object at 0x0000000001D91E00>
>a=re.compile(“^\([0-9]+),([0-9]+)\)$”)
>>>a.匹配(“(3,4)”)

在我看来,您似乎没有正确地转义构成坐标语法的括号。在正则表达式中,括号是。您需要通过以下方式逃离它们:

>>> a = re.compile("^\(([0-9]+), ([0-9]+)\)$")
>>> a.match("(3, 4)")
<_sre.SRE_Match object at 0x0000000001D91E00>
>a=re.compile(“^\([0-9]+),([0-9]+)\)$”)
>>>a.匹配(“(3,4)”)

在我看来,您似乎没有正确地转义构成坐标语法的括号。在正则表达式中,括号是。您需要通过以下方式逃离它们:

>>> a = re.compile("^\(([0-9]+), ([0-9]+)\)$")
>>> a.match("(3, 4)")
<_sre.SRE_Match object at 0x0000000001D91E00>
>a=re.compile(“^\([0-9]+),([0-9]+)\)$”)
>>>a.匹配(“(3,4)”)

在我看来,您似乎没有正确地转义构成坐标语法的括号。在正则表达式中,括号是。您需要通过以下方式逃离它们:

>>> a = re.compile("^\(([0-9]+), ([0-9]+)\)$")
>>> a.match("(3, 4)")
<_sre.SRE_Match object at 0x0000000001D91E00>
>a=re.compile(“^\([0-9]+),([0-9]+)\)$”)
>>>a.匹配(“(3,4)”)
这项工作:

from re import match
def coordinate(coord):   
    return bool(match("\s*\(\s*-?\d+(?:\.\d+)?\s*,\s*-?\d+(?:\.\d+)?\s*\)\s*$", coord))
它也非常强大,能够处理负数、分数和数字之间的可选空格

下面是正则表达式模式的细分:

\s*         # Zero or more whitespace characters
\(          # An opening parenthesis
\s*         # Zero or more whitespace characters
-?          # An optional hyphen (for negative numbers)
\d+         # One or more digits
(?:\.\d+)?  # An optional period followed by one or more digits (for fractions)
\s*         # Zero or more whitespace characters
,           # A comma
\s*         # Zero or more whitespace characters
-?          # An optional hyphen (for negative numbers)
\d+         # One or more digits
(?:\.\d+)?  # An optional period followed by one or more digits (for fractions)
\s*         # Zero or more whitespace characters
\)          # A closing parenthesis
\s*         # Zero or more whitespace characters
$           # End of the string
这项工作:

from re import match
def coordinate(coord):   
    return bool(match("\s*\(\s*-?\d+(?:\.\d+)?\s*,\s*-?\d+(?:\.\d+)?\s*\)\s*$", coord))
它也非常强大,能够处理负数、分数和数字之间的可选空格

下面是正则表达式模式的细分:

\s*         # Zero or more whitespace characters
\(          # An opening parenthesis
\s*         # Zero or more whitespace characters
-?          # An optional hyphen (for negative numbers)
\d+         # One or more digits
(?:\.\d+)?  # An optional period followed by one or more digits (for fractions)
\s*         # Zero or more whitespace characters
,           # A comma
\s*         # Zero or more whitespace characters
-?          # An optional hyphen (for negative numbers)
\d+         # One or more digits
(?:\.\d+)?  # An optional period followed by one or more digits (for fractions)
\s*         # Zero or more whitespace characters
\)          # A closing parenthesis
\s*         # Zero or more whitespace characters
$           # End of the string
这项工作:

from re import match
def coordinate(coord):   
    return bool(match("\s*\(\s*-?\d+(?:\.\d+)?\s*,\s*-?\d+(?:\.\d+)?\s*\)\s*$", coord))
它也非常强大,能够处理负数、分数和数字之间的可选空格

下面是正则表达式模式的细分:

\s*         # Zero or more whitespace characters
\(          # An opening parenthesis
\s*         # Zero or more whitespace characters
-?          # An optional hyphen (for negative numbers)
\d+         # One or more digits
(?:\.\d+)?  # An optional period followed by one or more digits (for fractions)
\s*         # Zero or more whitespace characters
,           # A comma
\s*         # Zero or more whitespace characters
-?          # An optional hyphen (for negative numbers)
\d+         # One or more digits
(?:\.\d+)?  # An optional period followed by one or more digits (for fractions)
\s*         # Zero or more whitespace characters
\)          # A closing parenthesis
\s*         # Zero or more whitespace characters
$           # End of the string
这项工作:

from re import match
def coordinate(coord):   
    return bool(match("\s*\(\s*-?\d+(?:\.\d+)?\s*,\s*-?\d+(?:\.\d+)?\s*\)\s*$", coord))
它也非常强大,能够处理负数、分数和数字之间的可选空格

下面是正则表达式模式的细分:

\s*         # Zero or more whitespace characters
\(          # An opening parenthesis
\s*         # Zero or more whitespace characters
-?          # An optional hyphen (for negative numbers)
\d+         # One or more digits
(?:\.\d+)?  # An optional period followed by one or more digits (for fractions)
\s*         # Zero or more whitespace characters
,           # A comma
\s*         # Zero or more whitespace characters
-?          # An optional hyphen (for negative numbers)
\d+         # One or more digits
(?:\.\d+)?  # An optional period followed by one or more digits (for fractions)
\s*         # Zero or more whitespace characters
\)          # A closing parenthesis
\s*         # Zero or more whitespace characters
$           # End of the string



首先,您需要的是
b=a.match(coord)
,而不是
b=re.match(coord)
。您需要转义正则表达式字符串中的一些括号。这里有一些参数,您指的是文字参数,但它们将被解释为捕获组。@TimPeters,但由于参数不正确,我预计会出现
TypeError
。。。所以我真的不确定我们是否在处理这里的实际代码…我如何逃离帕伦斯?我在这里输入错误,我在程序中使用了.match。修正了。如果你只是返回真/假,那么你可以只做
返回bool(a.match(coord))
。。。但是,无论如何返回match对象不会有太大的影响。首先,您需要
b=a.match(coord)
,而不是
b=re.match(coord)
。您需要转义regex字符串中的一些括号。这里有一些参数,您指的是文字参数,但它们将被解释为捕获组。@TimPeters,但由于参数不正确,我预计会出现
TypeError
。。。所以我真的不确定我们是否在处理这里的实际代码…我如何逃离帕伦斯?我在这里输入错误,我在程序中使用了.match。修正了。如果你只是返回真/假,那么你可以只做
返回bool(a.match(coord))
。。。但是,无论如何返回match对象不会有太大的影响。首先,您需要
b=a.match(coord)
,而不是
b=re.match(coord)
。您需要转义regex字符串中的一些括号。这里有一些参数,您指的是文字参数,但它们将被解释为捕获组。@TimPeters,但由于参数不正确,我预计会出现
TypeError
。。。所以我真的不确定我们是否在处理这里的实际代码…我如何逃离帕伦斯?我在这里输入错误,我在程序中使用了.match。修正了。如果你只是返回真/假,那么你可以只做
返回bool(a.match(coord))
。。。但是,无论如何返回match对象不会有太大的影响。首先,您需要
b=a.match(coord)
,而不是
b=re.match(coord)
。您需要转义regex字符串中的一些括号。这里有一些参数,您指的是文字参数,但它们将被解释为捕获组。@TimPeters,但由于参数不正确,我预计会出现
TypeError
。。。所以我真的不确定我们是否在处理这里的实际代码…我如何逃离帕伦斯?我在这里输入错误,我在程序中使用了.match。修正了。如果你只是返回真/假,那么你可以只做
返回bool(a.match(coord))
。。。然而,无论如何,返回匹配对象不会有太大的伤害,也许一个可选的空格会更好。很好的解释<代码>\s?对我来说似乎很奇怪-它匹配一个空格、一个制表符、一个换行符或任何其他单个空格字符,但不匹配两个空格。匹配的制表符(而不是多个空格)感觉不一致。如果我在编写正则表达式,我可能会选择<代码> 040 > <代码>或<代码> **/COD>。考虑一个可选的引导和结束空间;i、 e.:
“\s*\(?\d+(?:\。\d+),\s-?\d+(?:\。\d+))\s*$”
谢谢你的提示;我已经把它们写进我的帖子里了。当构建复杂的东西时,它肯定会