Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果我的字符串“deed”在前 9 个字符中包含“数字或空白”,我正在尝试测试 True。
deed = "4472 0438 (N/A Online)$0"
我尝试了以下几种变体,但当我在不应返回 True 的字符串上尝试它时,总是返回 True。所以我做错了什么。
re.search("([\. 0-9]{0,6})",deed)
任何建议将不胜感激!抱歉,Regex 是全新的......但是正在学习。
只需切片前九个字母:
re.search(expr, deed[:9])
试试下面的正则表达式
re.search("(^[ 0-9]{9})", deed)
该表达式"(^[ 0-9]{9})"将字符串定位在行首,并且仅当前 9 个字符是数字或空白时才匹配。
"(^[ 0-9]{9})"