REGEXP_INSTR

最近更新时间: 2025-02-18 16:02:00

寻找字符第一个出现位置。

  • 复制
    复制成功
create or replace function regexp_instr(str text,str2 text,str3 INTEGER) returns text as 
$$
declare
    res text;
begin
    res = str3 - 1 + position(substring(substring(str, str3) from str2) in str);
    return res;
end;
$$
language plpgsql;
create or replace function regexp_instr(str text,str2 text) returns text as $$
declare
    res text;
begin
    res = position(substring(str from str2) in str);
    return res;
end;
$$
language plpgsql;

调用结果

  • 复制
    复制成功
#搜索第一次出现的位置
postgres=# select regexp_instr('abc123abc156', '[1-2]+')  from  dual;
 regexp_instr 
--------------
 4
(1 row)
#搜索第一次出现的位置
postgres=# select regexp_instr('hello Tbase','e');
 regexp_instr 
--------------
 2
(1 row)
#从第7个开始搜索
postgres=# select regexp_instr('abc123abc156', '[1-2]+', 7)  from  dual;
 regexp_instr 
--------------
 10
(1 row)