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

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

Since a . in a PostgreSQL regex matches any char including line break chars, you need to introduce two changes:

  • The first .* should be replaced with [^rn]+ to match any chars other than common line break chars
  • The [sS] in the lookahead should be replaced with a mere ..

You can use

(?<=Schedule : )[^rn]+(?!.*Schedule)

See the online demo:

SELECT REGEXP_MATCHES(
    ELane Closures : Lane one will be closednReason : Roadworks are plannednStatus : PendingnSchedule : Expect disruption everyday between 20:00 and 06:00 from 5 October 2020 to 9 October 2020nSchedule : Expect disruption everyday between 20:00 and 06:00 from 12 October 2020 to 16 October 2020nSchedule : Expect disruption everyday between 20:00 and 06:00 from 19 October 2020 to 23 October 2020nSchedule : Expect disruption everyday between 20:00 and 06:00 from 26 October 2020 to 31 October 2020nLanes Closed : There will be one of two lanes closed, 
    (?<=Schedule : )[^rn]+(?!.*Schedule), 
    g) 

Output:

enter

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

Related posts

Leave a Reply

Your email address will not be published.