Regex 使正则表达式在获取consequentive数字时更加具体
如上图所示,我有以下Regex 使正则表达式在获取consequentive数字时更加具体,regex,python-3.x,string,pandas,text,Regex,Python 3.x,String,Pandas,Text,如上图所示,我有以下df。我使用以下公式仅获取consequentive数字 import pandas as pd df= pd.DataFrame({'Data':['123456A122 119999 This 1234522261 1A1619 BL171111 A-1-24', '134456 dont 12-23-34-45-5-6 Z112 NOT 01-22-2001',
df
。我使用以下公式仅获取consequentive数字
import pandas as pd
df= pd.DataFrame({'Data':['123456A122 119999 This 1234522261 1A1619 BL171111 A-1-24',
'134456 dont 12-23-34-45-5-6 Z112 NOT 01-22-2001',
'mix: 1A25629Q88 or A13B ok'],
'IDs': ['A11','B22','C33'],
})
这收集了很多我不想要的东西,比如BL171111
中的171111
和123456
中的123456A122
等等
我想下面的输出,只拿起6个连续数字
reg = r'((?:[\d]-?){6,})'
df['new'] = df['Data'].str.findall(reg)
Data IDs new
0 [123456,119999, 1234522261, 171111]
1 [134456, 12-23-34-45-5-6, 01-22-2001]
2 []
如何将正则表达式更改为so
Data IDs new
0 [119999]
1 [134456]
2 []
将正则表达式更改为使用单词边界(
\b
),并将位数精确限制为6,如下所示:
reg = r'((?:[\d]-?){6,})'
这将查找单词边界、6个数字和另一个单词边界
这是一个.df.Data.str.split(expand=True).apply(pd.to_numeric,errors='concurve').bfill(1).iloc[:,0]?
reg = r'(\b\d{6}\b)'