如何使用正则表达式从字符串中删除结束日期? - SQL

发布于 2025-01-20 19:43:23 字数 608 浏览 5 评论 0原文

我想以特定格式格式化表列中的字符串。

输入表:

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/

我想删除最后一个 / 符号之后的所有内容,如果它后面唯一的内容类似于日期(即 YYYY-MM-dd 或 ####-##-##)。

输出:

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

我正在考虑做类似的事情:但

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

我不确定如何为此编写正则表达式。如果有的话,我也愿意接受更简单的字符串操作方法。提前致谢!

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 (i.e. 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疏忽 2025-01-27 19:43:23

您可以使用

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

regex demo

/[0-9] {4} - [0-9] {1,2} - [0-9] {1,2} $ 是POSIX ERE符合符合的模式匹配

  • / - slash
  • [0-9] {4} - 四位数
  • - - 连字符
  • [0-9] {1,2} - 一两个数字
  • - [0-9] {1,2} - 连字符和一两个数字
  • $ $ - 字符串的结尾。

如果您的值可以包含尾随的空格,请在 [:SPACE:]]*之前插入 $ /[0-9] {4} - [0- 9] {1,2} - [0-9] {1,2} [[:SPACE:]]*$

, '/' )

regex demo

/[0-9] {4} - [0-9] {1,2} - [0-9] {1,2} $是POSIX ERE符合符合的模式匹配

  • / - slash
  • [0-9] {4} - 四位数
  • - - 连字符
  • [0-9] {1,2} - 一两个数字
  • - [0-9] {1,2} - 连字符和一两个数字
  • $ $ - 字符串的结尾。

如果您的值可以包含尾随的空格,请在[:SPACE:]]*之前插入$/[0-9] {4} - [0- 9] {1,2} - [0-9] {1,2} [[:SPACE:]]*$

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:]]*$.

, '/' )

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:]]*$.

无边思念无边月 2025-01-27 19:43:23

您可以使用此正格:

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

您可以尝试此查询:

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

demo

,'$1')

demo

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}

Demo

,'$1')

Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文