Python正则表达式匹配和搜索

2024-04-19 05:13:35 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有这个正则表达式

[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 

但是,如果我使用regex,我将能够同时搜索2131421314123

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

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

Tags: 字符串标记importrehome列表searchmatch