PostgreSQL string character replacement

后端 未结 2 1535
夕颜
夕颜 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
    
    0 讨论(0)
  • 2021-01-26 04:22
    select
      root,
      root_i,
      translate(root_i, "123", array_to_string(root,'')) as word_i
    NATURAL JOIN tbl_patterns
    NATURAL JOIN tbl_patterns_triliteral
    where root is not null and root_i is not null;
    
    0 讨论(0)
提交回复
热议问题