简体   繁体   中英

How to strip ending date from string using Regex? - SQL

I want to format the strings in a table column, in a specific format.

Input table:

file_paths
my-file-path/wefw/wefw/2022-03-20
my-file-path/wefw/2022-01-02
my-file-path/wef/wfe/wefw/wef/2021-02-03
my-file-path/wef/wfe/wef/

I want to remove everything after the last / sign, if the only thing after it resembles a date (ie YYYY-MM-dd or ####-##-##).

Output:

file_paths
my-file-path/wefw/wefw/
my-file-path/wefw/
my-file-path/wef/wfe/wefw/wef/
my-file-path/wef/wfe/wef/

I'm thinking of doing something like:

SELECT regexp_replace(file_paths, 'regex_here', '', 1, 'i')
FROM my_table

I'm unsure of how to write the RegEx for this though. I'm also open to easier methods of string manipulation, if there are any. Thanks in advance!

You can use

REGEXP_REPLACE ( file_paths, '/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$', '/' )

See the regex demo .

The /[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$ is a POSIX ERE compliant pattern matching

  • / - a slash
  • [0-9]{4} - four digits
  • - - a hyphen
  • [0-9]{1,2} - one or two digits
  • -[0-9]{1,2} - a hyphen and one or two digits
  • $ - end of string.

If your values can contain trailing whitespace, insert [[:space:]]* before $ : /[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}[[:space:]]*$ .

You may use this regex:

^(.*?\/)\d{4}-\d{2}-\d{2}$

You may try this query:

select regexp_replace(file_paths, '^(.*?\/)\\d{4}-\\d{2}-\\d{2}$','$1')

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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