繁体   English   中英

Postgres Regex:在最后一次出现模式和行尾之后提取字符串

[英]Postgres Regex: extract string after the last occurence of the pattern and the end of line

请帮我提取最后一次出现Schedule:和行尾之后的文本。

Lane Closures : Lane one will be closed
Reason : Roadworks are planned
Status : Pending
Schedule : Expect disruption everyday between 20:00 and 06:00 from 5 October 2020 to 9 October 2020
Schedule : Expect disruption everyday between 20:00 and 06:00 from 12 October 2020 to 16 October 2020
Schedule : Expect disruption everyday between 20:00 and 06:00 from 19 October 2020 to 23 October 2020
Schedule : Expect disruption everyday between 20:00 and 06:00 from 26 October 2020 to 31 October 2020
Lanes Closed : There will be one of two lanes closed

在上述情况下,我需要在Expect disruption everyday between 20:00 and 06:00 from 26 October 2020 to 31 October 2020

到目前为止,我只想到了以下内容:

(?<=Schedule : ).*(?![\s\S]*Schedule)

但它在 Postgres 中不起作用。 它返回错误: invalid regular expression: invalid escape \ sequence

我还尝试根据Postgres 文档\s\S替换为[:space:]^[:space:]但它也不起作用

提前致谢。

由于一个. 在 PostgreSQL 正则表达式匹配任何字符包括换行字符,你需要引入两个变化:

  • 第一个.*应替换为[^\r\n]+以匹配除常见换行符以外的任何字符
  • 前瞻中的[\s\S]应仅替换为. .

您可以使用

(?<=Schedule : )[^\r\n]+(?!.*Schedule)

请参阅在线演示

SELECT REGEXP_MATCHES(
    E'Lane Closures : Lane one will be closed\nReason : Roadworks are planned\nStatus : Pending\nSchedule : Expect disruption everyday between 20:00 and 06:00 from 5 October 2020 to 9 October 2020\nSchedule : Expect disruption everyday between 20:00 and 06:00 from 12 October 2020 to 16 October 2020\nSchedule : Expect disruption everyday between 20:00 and 06:00 from 19 October 2020 to 23 October 2020\nSchedule : Expect disruption everyday between 20:00 and 06:00 from 26 October 2020 to 31 October 2020\nLanes Closed : There will be one of two lanes closed', 
    '(?<=Schedule : )[^\r\n]+(?!.*Schedule)', 
    'g') 

Output:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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