PostgreSQL string character replacement

后端 未结 2 1562
夕颜
夕颜 2021-01-26 03:32

I\'m trying to write a lexical database for storing words comprised of roots and patterns, and I was wondering how I could create a column that will combine the root and pattern

2条回答
  •  青春惊慌失措
    2021-01-26 04:11

    I must admit to not liking to do much string manipulation in sql/plpgsql functions. Perl has an operator for replacing regexp matches with generated replacements, which works fairly nicely:

    create or replace function splice_to_word(root text, root_i text)
      returns text strict immutable language plperl as $$
      my $roots = shift;
      my $template = shift;
      $template =~ s{(\d+)}{substr($roots,$1-1,1)}ge;
      return $template;
    $$;
    

    There is some nastiness in that postgresql arrays don't seem to be translated into Perl lists, so I've assumed the roots are passed in as a string, e.g.:

    select root, root_i, splice_to_word(array_to_string(root, ''), root_i) from data
    

提交回复
热议问题