使用正则表达式识别字母/数字组合并存储在字典中 [英] identify letter/number combinations using regex and storing in dictionary

查看:138
本文介绍了使用正则表达式识别字母/数字组合并存储在字典中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import pandas as pd
df = pd.DataFrame({'Date':['This 1-A16-19 person is BL-17-1111 and other',
                          'dont Z-1-12 do here but NOT 12-24-1981',
                          'numbers: 1A-256-29Q88 ok'], 
                  'IDs': ['A11','B22','C33'],
                  }) 

使用上面的dataframe我要执行以下操作1)使用regex标识所有数字和数字组合,例如1-A16-19 2)存储在字典中

Using the dataframe above I want to do the following 1) Use regex to identify all digit + number combination e.g 1-A16-19 2) Store in dictionary

理想情况下,我希望获得以下输出(请注意,由于正则表达式中没有带字母,例如1A-24-1981,因此12-24-1981有意未被正则表达式接受)

Ideally I would like the following output (note that 12-24-1981 intentionally was not picked up by the regex since it doesn't have a letter in it e.g. 1A-24-1981)

{1: 1-A16-19, 2:BL-17-1111, 3: Z-1-12, 4: 1A-256-29Q88}

有人可以帮我吗?

推荐答案

此正则表达式可以解决问题.

This regex might do the trick.

(?=.*[a-zA-Z])(\S+-\S+-\S+)

它匹配两个具有两个-的两个空格之间的所有内容.如果没有字母,也不会匹配.

It matches everything between two spaces that has two - in it. Also there won't be a match if there is no letter present.

regex101示例

如您所见,对于给定的输入,您仅提供了1-A16-19BL-17-1111Z-1-12& 1A-256-29Q88即将退货.

As you can see for the given input you provided only 1-A16-19, BL-17-1111, Z-1-12 & 1A-256-29Q88 are getting returned.

这篇关于使用正则表达式识别字母/数字组合并存储在字典中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆