如何使用正则表达式从字符串中提取前两个字符

前端之家收集整理的这篇文章主要介绍了如何使用正则表达式从字符串中提取前两个字符前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参考: Pandas DataFrame: remove unwanted parts from strings in a column

参考上面链接中提供的答案.我研究了一些正则表达式,我打算深入研究,但与此同时我可以使用一些帮助.

我的数据框是这样的:

DF:

c_contofficeID
0           0109
1           0109
2           3434
3         123434  
4         1255N9
5           0109
6         123434
7           55N9
8           5599
9           0109

Psuedo Code

如果前两个字符是12则删除它们.或者,在前两个字符中没有12的字符中添加12.

结果如下:

c_contofficeID
0           0109
1           0109
2           3434
3           3434  
4           55N9
5           0109
6           3434
7           55N9
8           5599
9           0109

我正在使用上面链接中的答案作为起点:

df['contofficeID'].replace(regex=True,inplace=True,to_replace=r'\D',value=r'')

我尝试过以下方法

尝试1)

df['contofficeID'].replace(regex=True,to_replace=r'[1][2]',value=r'')

尝试2)

df['contofficeID'].replace(regex=True,to_replace=r'$[1][2]',value=r'')

尝试3)

df['contofficeID'].replace(regex=True,to_replace=r'?[1]?[2]',value=r'')

解决方法

新的答案
来自@Addison的评论

# '12(?=.{4}$)' makes sure we have a 12 followed by exactly 4 something elses
df.c_contofficeID.str.replace('^12(?=.{4}$)','')

如果ID必须有四个字符,那么它就更简单了

df.c_contofficeID.str[-4:]

老答案
使用str.replace

df.c_contofficeID.str.replace('^12','').to_frame()

enter image description here

猜你在找的正则表达式相关文章