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

Python正则表达式匹配和搜索不起作用,python,regex,Python,Regex,假设我有这个正则表达式 [0-9 ]{5,7} 我正在使用python import re re.search(r'[0-9 ]{5,7}', '1123124213') 这将为此返回一个正值 然而,如果我使用 re.match(r'[0-9 ]{5,7}', '1123124213') 但是,我的字符串通常类似于: I am going home tonight call me at 21314123 标记化也不起作用。我如何解决这个问题 我想配这个 I am going home

假设我有这个正则表达式

[0-9 ]{5,7}
我正在使用python

import re
re.search(r'[0-9 ]{5,7}', '1123124213')
这将为此返回一个正值

然而,如果我使用

re.match(r'[0-9 ]{5,7}', '1123124213')
但是,我的字符串通常类似于:

I am going home tonight call me at 21314123 
标记化也不起作用。我如何解决这个问题

我想配这个

I am going home tonight call me at 21314
但不是

I am going home tonight call me at 21314123 
然而,如果我使用正则表达式,我将能够搜索
21314
21314123

基本上我有一个包含这些字符串的字符串列表

I am going home tonight call me at 213123 
I am going home tonight call me at 21313 
I am going home tonight call me at 2131 
I am going home tonight call me at 21314213123 
I am going home tonight call me at 21314 
我想使用regex/任何方法来提取那些包含

I am going home tonight call me at 21314

re.search
将在字符串中的任何位置找到您的模式,而
re.match
将只在开头找到它。因此,对于示例字符串,
re.match
不会返回值。

解决什么问题?有错误吗?@ase您能解释一下上面的逻辑吗?@aceminer-所以您只想捕获长度为5-7的数字?其他的应该被拒绝?@vrajs5是的,没错