简体   繁体   English

使用 sql 捕获模式的正则表达式

[英]Regex to capture pattern with sql

Given the cell data shown below is of column 'Feed'鉴于下面显示的单元格数据属于“Feed”列

hdfs//sddad/aa/vv/cc/SR_DC_EF_GF_20181130_20156478907000_484658274168_CO.dat

i am trying to use a regex method to only display this value ' SR_DC_EF_GF' .我正在尝试使用正则表达式方法来仅显示此值 ' SR_DC_EF_GF' Currently i am manually doing a regex method by date which i dont feel its dynamic enough.目前我正在按日期手动执行正则表达式方法,我觉得它不够动态。 e,g例如

select `regexp_replace([Feed], '_2018.*', '')` from tablename. 

this will only display and does regex on table that is _20181130.这只会在 _20181130 的表上显示并执行正则表达式。 but if i were to have _2019 and _2020, it wont capture and display the whole value.但是如果我有 _2019 和 _2020,它就不会捕获和显示整个值。 how we can make this regex method dynamic where it can capture other dates?我们如何使这个正则表达式方法动态化,它可以捕获其他日期?

You could look for four digits:您可以查找四位数:

select regexp_replace([Feed], '_[0-9]{4}.*', '')
from tablename;

However, you are only getting rid of the suffix.但是,您只是摆脱了后缀。 I think you want something like this to extract the piece you are looking for:我想你想要这样的东西来提取你正在寻找的作品:

select regexp_replace([Feed], '^.*([^/0-9]+)_[0-9]{4}.*$', '\1')
from tablename;

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

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