python RE vs html5 RE [英] python RE vs html5 RE

查看:308
本文介绍了python RE vs html5 RE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎我在python中遇到了一个错误:(Python 2.7.3(默认,2012年4月10日,23:24:47)[MSC v.1500 64位(AMD64)]在win32上)

it seems that i got a bug in python: (Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32)

>>> re.match("0[5-7][5-9][0-9]{7}", "0775123456") #match
<_sre.SRE_Match object at 0x0000000002D3FC60>

>>> re.match("0[5-7][5-9][0-9]{7}", "077512345") #dont match

>>> re.match("0[5-7][5-9][0-9]{7}", "07751234567") #match!
<_sre.SRE_Match object at 0x0000000002D3F920>

>>> re.match("0[5-7][5-9][0-9]{7}","07751234567777777777777777777777777777777777777777777777") #match!!
<_sre.SRE_Match object at 0x0000000002D3FC60>

因此我必须使用美元符号来限制确切数字!
i认为这是一个错误,因为在HTML5验证中,{7}意味着完全7位数,而在python中,它似乎意味着至少
,这就是它的行为方式美元符号:

so i must use the dollar sign to limit "exactly" the numer! i think it's a bug, because in HTML5 validation, the {7} means "exactly" 7 digits, and here in python, it seems that it means "at least" and here is how it behaves using the dollars sign:

>>> re.match("0[5-7][5-9][0-9]{7}$", "0775123456") #match
<_sre.SRE_Match object at 0x0000000002D3F920>

>>> re.match("0[5-7][5-9][0-9]{7}$", "07751234567") #dont match

>>> re.match("0[5-7][5-9][0-9]{7}$", "077512345") #dont match

这种情况发生在所有其他正则表达式上,不仅是确切的数字匹配,还必须加上美元!

and this happens to all other regular expression, not only the exact number matching, the dollar must be added!

这是一个错误吗?或者它是按设计的?

so is it a bug? or it's by design?

推荐答案

这是设计的。 re.match 匹配字符串的开头而不是 re.search ,它匹配字符串中的任何位置。字符串后面的额外字符将被忽略。请参见 http://docs.python.org/library/re.html#match更多详细信息。

This is by design. re.match matches at the beginning of a string as opposed to re.search, which matches anywhere in a string. Extra characters after the string are ignored. See http://docs.python.org/library/re.html#match for more details.

其他使用正则表达式的语言(如grep和perl)的行为相同。正则表达式主要用于搜索文本。

Other languages that also use regular expressions, such as grep and perl, act the same. Regular expressions are primarily used for searching text.

如果你想进行完全匹配,你必须自己指定美元符号。

If you want to perform an exact match you have to specify the dollar sign as you yourself also noted.

这篇关于python RE vs html5 RE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆