Re: Regex help again (sorry, I am bad at these)

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Christopher Molnar <cmolnar(at)ourworldservices(dot)com>
Cc: PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Re: Regex help again (sorry, I am bad at these)
Date: 2015-12-28 19:41:06
Message-ID: CAKFQuwZ_1PC-SabOzn8knMOowDEiSkaexsi+9TpG3kGie1DhyQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, Dec 28, 2015 at 12:10 PM, Christopher Molnar <
cmolnar(at)ourworldservices(dot)com> wrote:

Given this...

> '<p>Complete the attached lab and submit via dropbox</p>\r<p><a href="
> https://owncloud.porterchester.edu/HVACR/PCI_GasHeat/GasElectrical/HVACR1114_LAB_13A.pdf"
> title="Lab 13A">Lab 13A<\a>'
>

​I have no clue how the following gives you any matches...​
specifically the presence of the "$" after the title= causes the entire
pattern to always fail since that isn't the end of the string.

> ​
>
> update pcilms_assign set intro=regexp_replace(intro, '/([^/]*)\"
> title=$', '&files=\1') where intro like '%
> https://owncloud.porterchester.edu%' and course=18 and id=55413;
>
> and the result puts the &file= in the wrong place (at the end of the whole
> string).
>
​The basic problem is that entirety of the content that your pattern
matches ​is replaced with the totality of the replacement expression.
Since you are matching the literal "title=" you have to somehow place that
same literal in the result. You can capture it and then use "\2" or you
can place it literally like Félix shows.

Alternatively, don't capture it. The way you match something without
capturing it is by using what is termed a "zero-width" expression or a
"look-around". In this case you want to "look-ahead" which is expressed
thusly: (?=)

So...

'/([^/]*)(?=" title=)'

SELECT regexp_replace('<a href="https://www.www.www/path/FILE.pdf"
title="FILE">', '/([^/]*)(?=" title=)', '&files=\1')

David J.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2015-12-28 19:42:23 Re: Regex help again (sorry, I am bad at these)
Previous Message Jeff Janes 2015-12-28 19:35:04 Re: grep -f keyword data query