Re: [SQL] Plpgsql: Assign regular expression match to variable

2009-09-01 Thread Leif B. Kristensen
On Tuesday 1. September 2009, Ian Barwick wrote: >This seems to do what you want: > > my_int := (REGEXP_MATCHES(txt, E'^#(\\d+)'))[1]; Great! I had no idea that REGEXP_MATCHES() could do that kind of stuff. pgslekt=> select (REGEXP_MATCHES('#42 blabla', E'^#(\\d+)')) [1]::integer; regexp_match

Re: [SQL] Plpgsql: Assign regular expression match to variable

2009-09-01 Thread Ian Barwick
2009/9/1, Leif B. Kristensen : > In Plpgsql, I've got this problem of how to assign an integer extracted > from a regex to a variable. My approach so far feels kludgy: > > -- extract ^#(\d+) from txt > IF txt SIMILAR TO E'#\\d+%' THEN > my_int := SUBSTR(SUBSTRING(txt, E'#\\d+'), 2, > LE

[SQL] Plpgsql: Assign regular expression match to variable

2009-09-01 Thread Leif B. Kristensen
In Plpgsql, I've got this problem of how to assign an integer extracted from a regex to a variable. My approach so far feels kludgy: -- extract ^#(\d+) from txt IF txt SIMILAR TO E'#\\d+%' THEN my_int := SUBSTR(SUBSTRING(txt, E'#\\d+'), 2, LENGTH(SUBSTRING(txt, E'#\\d+')) -1)::INTEGER