regex PostgreSQL正则表达式_替换替换2个组

vc9ivgsu  于 2022-11-26  发布在  PostgreSQL
关注(0)|答案(1)|浏览(216)

我在Postgres数据库中有一个列,它有char varying数据类型的文本。文本包括一个包含文件名的uri,如下所示;

The file is a file of \\88-77-99-666.abc.example.com\Folder1\Folder2\Folder3\Folder4\20221122\12345678.PDF [9bc8rer55c655f4cb5df763c61862d3fdde9557b0] is the sha1 of the file.

我试图从文本内容中获取文件名12345678.PDF和日期20221122。但是,regexp_replace要么提供文件名之前的所有内容,要么提供文件名之后的所有内容。我试图只获取文件名

1>> Regexp_replace(data, '.+\\', '')

生成文件名及其后面的所有内容

2>> Regexp_replace(data, '\[.*', '')

生成文件名及其后面的所有内容
如果我捕获两个组,如下所示,我得到的结果与1相同。

Regexp_replace(data, '.+\\|\[', '')

我怎么能替换2组,只得到文件名?或者什么是更好的方式来实现这一点?我需要得到日期值,但如果我能弄清楚这一点,也许我将能够应用学习来提取日期值。感谢您的时间。

qhhrdooz

qhhrdooz1#

您可以使用

SELECT REGEXP_MATCHES(
  'The file is a file of \\88-77-99-666.abc.example.com\Folder1\Folder2\Folder3\Folder4\20221122\2779780.PDF [9bc8rer55c655f4cb5df763c61862d3fdde9557b0] is the sha1 of the file.',
  '([^[:space:]\\/]+)\s+\[([^][]+)') AS Result;

参见DB fiddle,结果:

  • 详细数据 *:
  • ([^[:space:]\\/]+)-组1:除\/和空白之外的一个或多个字符
  • \s+-一个或多个空格
  • 一个\[字符
  • ([^][]+)-组2:[]以外的一个或多个字符。

相关问题