<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        PostgreSQL正則表達式常用函數的總結

        來源:懂視網 責編:小采 時間:2020-11-09 20:31:33
        文檔

        PostgreSQL正則表達式常用函數的總結

        PostgreSQL正則表達式常用函數的總結:PostgreSQL 正則表達式 常用函數的總結 對那些需要進行復雜數據處理的程序來說,正則表達式無疑是一個非常有用的工具。本文重點在于闡述 PostgreSQL 的一些常用正則表達式函數以及源碼中的一些函數。 正則相關部分的目錄結構 [root@localhos
        推薦度:
        導讀PostgreSQL正則表達式常用函數的總結:PostgreSQL 正則表達式 常用函數的總結 對那些需要進行復雜數據處理的程序來說,正則表達式無疑是一個非常有用的工具。本文重點在于闡述 PostgreSQL 的一些常用正則表達式函數以及源碼中的一些函數。 正則相關部分的目錄結構 [root@localhos

        PostgreSQL 正則表達式 常用函數的總結

        對那些需要進行復雜數據處理的程序來說,正則表達式無疑是一個非常有用的工具。本文重點在于闡述 PostgreSQL 的一些常用正則表達式函數以及源碼中的一些函數。

        正則相關部分的目錄結構

        [root@localhost regex]# pwd
        /opt/hgdb-core/src/include/regex
        [root@localhost regex]# ll
        total 40
        -rw-r--r--. 1 postgres postgres 3490 Mar 19 19:00 regcustom.h
        -rw-r--r--. 1 postgres postgres 1332 Mar 19 18:59 regerrs.h
        -rw-r--r--. 1 postgres postgres 6703 Mar 19 19:00 regex.h
        -rw-r--r--. 1 postgres postgres 2353 Mar 19 19:00 regexport.h
        -rw-r--r--. 1 postgres postgres 16454 Mar 19 19:00 regguts.h
        

         正則表達式編譯、匹配、釋放、錯誤信息相關文件,后面再做具體介紹

        [root@localhost regex]# pwd
        /opt/hgdb-core/src/backend/regex
        [root@localhost regex]# ll reg*.c
        -rw-r--r--. 1 postgres postgres 55851 Mar 19 19:00 regcomp.c
        -rw-r--r--. 1 postgres postgres 3671 Mar 19 18:59 regerror.c
        -rw-r--r--. 1 postgres postgres 34873 Mar 19 19:00 regexec.c
        -rw-r--r--. 1 postgres postgres 2123 Mar 19 18:59 regfree.c
        [root@localhost regex]# 
        

         內置函數實現在 regexp.c

        [root@localhost adt]# pwd
        /opt/hgdb-core/src/backend/utils/adt
        [root@localhost adt]# ll regexp.c
        -rw-r--r--. 1 postgres postgres 34863 Apr 12 02:29 regexp.c
        [root@localhost adt]#
        

        內置函數聲明:

        /* src/include/catalog/pg_proc.h */
        
        DATA(insert OID = 2073 ( substring PGNSP PGUID 12 1 0 0 0 f f f f t f i 2 0 25 "25 25" _null_ _null_ _null_ _null_ _null_ textregexsubstr _null_ _null_ _null_ ));
        DESCR("extract text matching regular expression");
        DATA(insert OID = 2074 ( substring PGNSP PGUID 14 1 0 0 0 f f f f t f i 3 0 25 "25 25 25" _null_ _null_ _null_ _null_ _null_ "select pg_catalog.substring($1, pg_catalog.similar_escape($2, $3))" _null_ _null_ _null_ ));
        DESCR("extract text matching SQL99 regular expression");
        
        DATA(insert OID = 2284 ( regexp_replace PGNSP PGUID 12 1 0 0 0 f f f f t f i 3 0 25 "25 25 25" _null_ _null_ _null_ _null_ _null_ textregexreplace_noopt _null_ _null_ _null_ ));
        DESCR("replace text using regexp");
        DATA(insert OID = 2285 ( regexp_replace PGNSP PGUID 12 1 0 0 0 f f f f t f i 4 0 25 "25 25 25 25" _null_ _null_ _null_ _null_ _null_ textregexreplace _null_ _null_ _null_ ));
        DESCR("replace text using regexp");
        
        DATA(insert OID = 2763 ( regexp_matches PGNSP PGUID 12 1 1 0 0 f f f f t t i 2 0 1009 "25 25" _null_ _null_ _null_ _null_ _null_ regexp_matches_no_flags _null_ _null_ _null_ ));
        DESCR("find all match groups for regexp");
        DATA(insert OID = 2764 ( regexp_matches PGNSP PGUID 12 1 10 0 0 f f f f t t i 3 0 1009 "25 25 25" _null_ _null_ _null_ _null_ _null_ regexp_matches _null_ _null_ _null_ ));
        DESCR("find all match groups for regexp");
        
        DATA(insert OID = 2765 ( regexp_split_to_table PGNSP PGUID 12 1 1000 0 0 f f f f t t i 2 0 25 "25 25" _null_ _null_ _null_ _null_ _null_ regexp_split_to_table_no_flags _null_ _null_ _null_ ));
        DESCR("split string by pattern");
        DATA(insert OID = 2766 ( regexp_split_to_table PGNSP PGUID 12 1 1000 0 0 f f f f t t i 3 0 25 "25 25 25" _null_ _null_ _null_ _null_ _null_ regexp_split_to_table _null_ _null_ _null_ ));
        DESCR("split string by pattern");
        
        DATA(insert OID = 2767 ( regexp_split_to_array PGNSP PGUID 12 1 0 0 0 f f f f t f i 2 0 1009 "25 25" _null_ _null_ _null_ _null_ _null_ regexp_split_to_array_no_flags _null_ _null_ _null_ ));
        DESCR("split string by pattern");
        DATA(insert OID = 2768 ( regexp_split_to_array PGNSP PGUID 12 1 0 0 0 f f f f t f i 3 0 1009 "25 25 25" _null_ _null_ _null_ _null_ _null_ regexp_split_to_array _null_ _null_ _null_ ));
        
        

         參數類型及返回值類型:

        postgres=# select oid,typname from pg_type where oid = 25 or oid = 1009;
         oid | typname 
        ------+---------
         25 | text
         1009 | _text
        (2 rows)
        

        substring(string from pattern)函數提供了從字符串中抽取一個匹配 POSIX 正則表達式模式的子字符串的方法。如果沒有匹配它返回 NULL ,否則就是文本中匹配模式的那部分。

        regexp_replace(source, pattern, replacement [, flags ])函數提供了將匹配 POSIX 正則表達式模式的子字符串替換為新文本的功能。

        regexp_matches(string, pattern[, flags ])函數返回一個從匹配POSIX正則表達式模式中獲取的所有子串結果的text數組。
        參數flags是一個可選的text字符串,含有0或者更多單字母標記來改變函數行為。標記g導致查找字符串中的每個匹配,而不僅是第一個,每個匹配返回一行。

        regexp_split_to_table(string, pattern[, flags ])函數使用POSIX正則表達式模式作為分隔符,分隔字符串。返回結果為string。

        regexp_split_to_array (string, pattern[, flags ])函數與regexp_split_to_table行為相同,但,返回結果為text數組。

        具體使用參考用戶手冊。

         src/include/regex/regex.h

         regex_t 結構體

        /* the biggie, a compiled RE (or rather, a front end to same) */
        typedef struct
        {
         int re_magic; /* magic number */
         size_t re_nsub; /* number of subexpressions */
         long re_info; /* information about RE */
        #define REG_UBACKREF 000001
        #define REG_ULOOKAHEAD 000002
        #define REG_UBOUNDS 000004
        #define REG_UBRACES 000010
        #define REG_UBSALNUM 000020
        #define REG_UPBOTCH 000040
        #define REG_UBBS 000100
        #define REG_UNONPOSIX 000200
        #define REG_UUNSPEC 000400
        #define REG_UUNPORT 001000
        #define REG_ULOCALE 002000
        #define REG_UEMPTYMATCH 004000
        #define REG_UIMPOSSIBLE 010000
        #define REG_USHORTEST 020000
         int re_csize; /* sizeof(character) */
         char *re_endp; /* backward compatibility kludge */
         Oid re_collation; /* Collation that defines LC_CTYPE behavior */
         /* the rest is opaque pointers to hidden innards */
         char *re_guts; /* `char *' is more portable than `void *' */
         char *re_fns;
        } regex_t;
        

        存放編譯后的正則表達式

        regmatch_t 結構體

        /* result reporting (may acquire more fields later) */
        typedef struct
        {
         regoff_t rm_so; /* start of substring */
         regoff_t rm_eo; /* end of substring */
        } regmatch_t;
        
        typedef long regoff_t;
        

        成員rm_so 存放匹配文本串在目標串中的開始位置,rm_eo 存放結束位置。通常我們以數組的形式定義一組這樣的結構。

        有下面幾個主要的函數聲明

        /*
         * the prototypes for exported functions
         */
        extern int pg_regcomp(regex_t *, const pg_wchar *, size_t, int, Oid);
        extern int pg_regexec(regex_t *, const pg_wchar *, size_t, size_t, rm_detail_t *, size_t, regmatch_t[], int);
        extern int pg_regprefix(regex_t *, pg_wchar **, size_t *);
        extern void pg_regfree(regex_t *);
        extern size_t pg_regerror(int, const regex_t *, char *, size_t);
        extern void pg_set_regex_collation(Oid collation);
        

        處理正則表達式常用的函數有 pg_regcomp()、pg_regexec()、pg_regfree() 和 pg_regerror()。

        一般處理步驟:編譯正則表達式 pg_regcomp(),匹配正則表達式 pg_regexec(),釋放正則表達式 pg_regfree()。

        pg_regerror() :當執行regcomp 或者regexec 產生錯誤的時候,就可以調用這個函數而返回一個包含錯誤信息的字符串。

        參數說明

        int
        pg_regcomp(regex_t *re,
         const chr *string, /* 正則表達式字符串 */
         size_t len, /* 正則表達式字符串長度 */
         int flags,
         Oid collation)
        
        int
        pg_regexec(regex_t *re, /* 已經用regcomp函數編譯好的正則表達式 */
         const chr *string, /* 目標字符串 */
         size_t len, /* 目標字符串長度 */
         size_t search_start, /* 匹配開始位置 */
         rm_detail_t *details, /* NULL */
         size_t nmatch, /* 是regmatch_t結構體數組的長度 */
         regmatch_t pmatch[], /* regmatch_t類型的結構體數組,存放匹配文本串的位置信息 */
         int flags)
        
        

        flags

        src/backend/utils/adt/regexp.c

        /* all the options of interest for regex functions */
        typedef struct pg_re_flags
        {
         int cflags; /* compile flags for Spencer's regex code */
         bool glob; /* do it globally (for each occurrence) */
        } pg_re_flags;
        /*
         * parse_re_flags - parse the options argument of regexp_matches and friends
         *
         * flags --- output argument, filled with desired options
         * opts --- TEXT object, or NULL for defaults
         *
         * This accepts all the options allowed by any of the callers; callers that
         * don't want some have to reject them after the fact.
         */
        static void
        parse_re_flags(pg_re_flags *flags, text *opts)
        {
         /* regex flavor is always folded into the compile flags */
         flags->cflags = REG_ADVANCED;
         flags->glob = false;
        
         if (opts)
         {
         char *opt_p = VARDATA_ANY(opts);
         int opt_len = VARSIZE_ANY_EXHDR(opts);
         int i;
        
         for (i = 0; i < opt_len; i++)
         {
         switch (opt_p[i])
         {
         case 'g':
         flags->glob = true;
         break;
         case 'b': /* BREs (but why???) */
         flags->cflags &= ~(REG_ADVANCED | REG_EXTENDED | REG_QUOTE);
         break;
         case 'c': /* case sensitive */
         flags->cflags &= ~REG_ICASE;
         break;
         case 'e': /* plain EREs */
         flags->cflags |= REG_EXTENDED;
         flags->cflags &= ~(REG_ADVANCED | REG_QUOTE);
         break;
         case 'i': /* case insensitive */
         flags->cflags |= REG_ICASE;
         break;
         case 'm': /* Perloid synonym for n */
         case 'n': /* \n affects ^ $ . [^ */
         flags->cflags |= REG_NEWLINE;
         break;
         case 'p': /* ~Perl, \n affects . [^ */
         flags->cflags |= REG_NLSTOP;
         flags->cflags &= ~REG_NLANCH;
         break;
         case 'q': /* literal string */
         flags->cflags |= REG_QUOTE;
         flags->cflags &= ~(REG_ADVANCED | REG_EXTENDED);
         break;
         case 's': /* single line, \n ordinary */
         flags->cflags &= ~REG_NEWLINE;
         break;
         case 't': /* tight syntax */
         flags->cflags &= ~REG_EXPANDED;
         break;
         case 'w': /* weird, \n affects ^ $ only */
         flags->cflags &= ~REG_NLSTOP;
         flags->cflags |= REG_NLANCH;
         break;
         case 'x': /* expanded syntax */
         flags->cflags |= REG_EXPANDED;
         break;
         default:
         ereport(ERROR,
         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
         errmsg("invalid regexp option: \"%c\"",
         opt_p[i])));
         break;
         }
         }
         }
        }
        
        

        選項 描述
        b 剩余的正則表達式是 BR
        c 大小寫敏感匹配(覆蓋操作符類型)
        e 剩余的正則表達式是 ERE 
        i 大小寫不敏感匹配(覆蓋操作符類型)
        m n的歷史同義詞
        n 新行敏感匹
        p 部分新行敏感匹配
        q 重置正則表達式為一個文本("引起")字符串,所有都是普通字符。
        s 非新行敏感匹配(缺省)
        t 緊語法
        w 反轉部分新行敏感("怪異")匹配
        x 擴展的語法

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        PostgreSQL正則表達式常用函數的總結

        PostgreSQL正則表達式常用函數的總結:PostgreSQL 正則表達式 常用函數的總結 對那些需要進行復雜數據處理的程序來說,正則表達式無疑是一個非常有用的工具。本文重點在于闡述 PostgreSQL 的一些常用正則表達式函數以及源碼中的一些函數。 正則相關部分的目錄結構 [root@localhos
        推薦度:
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 国产精品免费久久| 青娱乐在线视频免费观看| 在线涩涩免费观看国产精品| 亚洲天堂中文字幕在线| 国产无遮挡色视频免费观看性色| 亚洲 无码 在线 专区| 人人爽人人爽人人片av免费| 免费一级肉体全黄毛片| 黄 色一级 成 人网站免费| 在线观看亚洲精品福利片| 成在人线av无码免费高潮喷水| 亚洲精品私拍国产福利在线| 国产成人精品免费视频大| 亚洲精品又粗又大又爽A片| 国产成人无码免费视频97| 五月天国产成人AV免费观看| 久久亚洲国产午夜精品理论片| 蜜桃成人无码区免费视频网站| 91嫩草亚洲精品| 天天操夜夜操免费视频| 免费手机在线看片| 久久久久亚洲av无码专区蜜芽| 18未年禁止免费观看| 在线观看亚洲AV每日更新无码 | 国产成人综合亚洲| 久久亚洲欧洲国产综合| 久久午夜伦鲁片免费无码| 色偷偷女男人的天堂亚洲网| 国产精品国产免费无码专区不卡| 一级毛片成人免费看a| 久久亚洲AV成人无码软件| 免费羞羞视频网站| 中国性猛交xxxxx免费看| 亚洲毛片基地日韩毛片基地| 国产色爽免费视频| 久久精品无码精品免费专区| 亚洲最大的成人网站| 国产亚洲综合成人91精品| 成人免费看吃奶视频网站| 黄页免费在线观看| 亚洲AV色欲色欲WWW|