【问题标题】:How to get in postgresql using regex the substring on the left or right to the nth delimiter如何使用正则表达式进入postgresql左侧或右侧的子字符串到第n个分隔符
【发布时间】:2013-12-19 02:14:39
【问题描述】:

假设我有一个变量字符串,例如:

"Hello. Now is the time. Before. After "

我可以在 regexp_replace (Postgres) 中使用什么正则表达式来获取第二个或第 n 个分隔符左侧/右侧的子字符串,假设是“。”作为分隔符:

如果我想要第二个“。”的左侧字符串我应该得到:

"Now is the time"

如果我想要第二个“。”的右侧字符串。我应该得到:

"Before"

这是在 Postgresql 8.1 上(我知道...)

【问题讨论】:

    标签: regex postgresql


    【解决方案1】:

    我使用 string_to_array() 解决了我的问题:

    从第二个“.”获取左侧字符串:

    SELECT (STRING_TO_ARRAY('Hello. Now is the time. Before. After ', '.'))[2];
     string_to_array  
    ------------------
      Now is the time
    

    从第二个“.”获取右边的字符串:

    SELECT (STRING_TO_ARRAY('Hello. Now is the time. Before. After ', '.'))[3];
     string_to_array 
    -----------------
      Before
    

    虽然我想知道如何使用 regexp_replace() 来做到这一点,因为我怀疑它可能会更快。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-10
      • 2019-02-28
      • 2023-03-28
      • 2022-07-26
      • 2023-04-10
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多