SQL 正则表达式到 select 第二个和第三个正斜杠之间的字符串

SQL Regex to select string between second and third forward slash

我正在使用 Postgres/Redshift 查询 table 个 URL 并尝试使用 SELECT regex_substr 到 select 列中第二个和第三个正斜杠之间的字符串。

例如,我需要以下数据中的第二个斜杠分隔字符串:

/abc/required_string/5856365/
/abc/required_string/2/
/abc/required_string/l-en/
/abc/required_string/l-en/

遵循此 this thread 中的一些正则表达式:

SELECT regexp_substr(column, '/[^/]*/([^/]*)/')
FROM table

None 似乎有效。我不断收到:

/abc/required_string/
/abc/required_string/

这可能有效:

SQL Fiddle

PostgreSQL 9.3 架构设置:

CREATE TABLE t
    ("c" varchar(29))
;

INSERT INTO t
    ("c")
VALUES
    ('/abc/required_string/5856365/'),
    ('/abc/required_string/2/'),
    ('/abc/required_string/l-en/'),
    ('/abc/required_string/l-en/')
;

查询 1:

SELECT substring("c" from '/[^/]*/([^/]*)/')
FROM t

Results:

|       substring |
|-----------------|
| required_string |
| required_string |
| required_string |
| required_string |

split_part呢?

SELECT split_part(column, '/', 3) FROM table

示例:

select split_part ('/abc/required_string/2/', '/', 3)

Returns: required string