regexp question

Started by Andy Krigerabout 23 years ago3 messagesgeneral
Jump to latest
#1Andy Kriger
akriger@greaterthanone.com

I see that it's possible to select using regexps.
Is it possible to use portions of the selection in the result?

example...
select * from table where column ~ '.*\(blah.*\)'
will return any row with characters-blah-characters

Is there any way I can get just the blah.* portion of the string in my
result?

thx
andy

#2Peter Eisentraut
peter_e@gmx.net
In reply to: Andy Kriger (#1)
Re: regexp question

Andy Kriger writes:

select * from table where column ~ '.*\(blah.*\)'
will return any row with characters-blah-characters

Is there any way I can get just the blah.* portion of the string in my
result?

Use the substring function. See the documentation on pattern matching.

--
Peter Eisentraut peter_e@gmx.net

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Andy Kriger (#1)
Re: regexp question

"Andy Kriger" <akriger@greaterthanone.com> writes:

I see that it's possible to select using regexps.
Is it possible to use portions of the selection in the result?

The POSIX version of substring() might do what you want:
http://www.ca.postgresql.org/users-lounge/docs/7.3/postgres/functions-matching.html#FUNCTIONS-POSIX-REGEXP
I forget whether that's available in any version before 7.3.

If not, the next step would be to write a little function in plperl or
pltcl to mash the string however you want ...

regards, tom lane