简体   繁体   English

Python正则表达式匹配不起作用

[英]Python regular expression match not working

I have written below given code to match a string of fixed length of 10 which contains all digits. 我在下面的给定代码中编写了匹配包含所有数字的固定长度10的字符串。

import re
result=re.match("^d{10}$", u"5478512045")

But it returns None. 但是它返回None。 I don't know why is it failing. 我不知道为什么会失败。 Please correct me if I am doing anything wrong over here. 如果我在这里做错了什么,请纠正我。

You are missing an escape \\ on the d control character. 您在d控制字符上缺少转义\\ It should be: 它应该是:

result=re.match("^\d{10}$", u"5478512045")

Without the \\ before the d , your regex is trying to match a literal d string. d之前没有\\ ,您的正则表达式试图匹配文字d字符串。 By changing this to \\d you match against the special character for any decimal digit. 通过将其更改为\\d您将与特殊字符匹配任何十进制数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM