简体   繁体   English

使用 Python 正则表达式从字符串中提取 substring

[英]Extract substring from a string using Python regex

I am working with data frames.我正在处理数据框。 One of my column look like this我的专栏之一看起来像这样

                   X
4028    Toolx-2022-01-18_15-26-09.blf.26
4029    Temperaturelow-2022-01-18_15-26-09.blf.27
4031    Temperaturehigh2022-01-18_15-26-09.blf.28
4032    low2022-01-18_15-26-09.blf.29
4032    high2022-01-18_15-26-09.blf.30
Name: X, dtype: object

I want my output like this我想要这样的 output

                  X
4028    2022-01-18 15-26-09 26
4029    2022-01-18 15-26-09 27
4030    2022-01-18 15-26-09 28
4031    2022-01-18 15-26-09 29
4032    2022-01-18 15-26-09 30
Name: X, dtype: object 

Can anyone please help me how to do this in Python?任何人都可以帮助我如何在 Python 中执行此操作吗?

We can use str.extract here:我们可以在这里使用str.extract

df["X"] = df["X"].str.extract(r'(\d{4}-\d{1,2}-\d{1,2})_(\d{2}-\d{2}-\d{2}).*\.(\d+)', r'\1 \2 \3')

Here is a regex demo .这是一个正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM