雷火电竞-中国电竞赛事及体育赛事平台

    1. <small id='7xloxquh'></small><noframes id='g7ucetk2'>

      <tfoot id='ho1yun1u'></tfoot>
        • <bdo id='kl5p1ywp'></bdo><ul id='bq8ts5oi'></ul>

      1. <i id='4v2pdcdq'><tr id='3x4002yj'><dt id='facpt1rx'><q id='2hay1dhr'><span id='0x54m4ec'><b id='64zcesz2'><form id='ftz0rroy'><ins id='gxodqc21'></ins><ul id='wfa4svez'></ul><sub id='qmkp2nso'></sub></form><legend id='x0jdkdin'></legend><bdo id='la823x9s'><pre id='zwcmcodb'><center id='8memebla'></center></pre></bdo></b><th id='kn1yu330'></th></span></q></dt></tr></i><div class="l4l4wckh0sl" id='7l6u5rl0'><tfoot id='s663wu51'></tfoot><dl id='a5aoyuqe'><fieldset id='l6djvyfi'></fieldset></dl></div>
        <legend id='kfhxkxq4'><style id='6z16aebi'><dir id='8qe9ho0o'><q id='mj82rlfg'></q></dir></style></legend>
        歡迎來到入門教程網!

        C語言

        當前位置:主頁 > 軟件編程 > C語言 >

        c語言的正則匹配函數 c語言正則表達式函數庫

        來源:本站原創(chuàng)|時間:2023-04-02|欄目:C語言|點擊:

        C語言怎么用正則表達式

        由于它可以極大地簡化處理字符串時的復雜度,因此現在已經在許多 L i n u x 實用工具中得到了應用。千萬不要以為正則表達式只是 P e r l 、 P y t h o n 、 B a s h 等腳本語言的專利,作為 C 語言程序員,用戶同樣可以在自己的程序中運用正則表達式。標準的 C 和 C + + 都不支持正則表達式,但有一些函數庫可以輔助 C / C + + 程序員完成這一功能,其中最著名的當數 P h i l i p H a z e l 的 P e r l - C o m p a t i b l e R e g u l a r E x p r e s s i o n 庫,許多 L i n u x 發(fā)行版本都帶有這個函數庫。編譯正則表達式為了提高效率,在將一個字符串與正則表達式進行比較之前,首先要用 r e g c o m p ( ) 函數對它進行編譯,將其轉化為 r e g e x _ t 結構: i n t r e g c o m p ( r e g e x _ t * p r e g , c o n s t c h a r * r e g e x , i n t c f l a g s ) ; 參數 r e g e x 是一個字符串,它代表將要被編譯的正則表達式;參數 p r e g 指向一個聲明為 r e g e x _ t 的數據結構,用來保存編譯結果;參數 c f l a g s 決定了正則表達式該如何被處理的細節(jié)。如果函數 r e g c o m p ( ) 執(zhí)行成功,并且編譯結果被正確填充到 p r e g 中后,函數將返回 0 ,任何其它的返回結果都代表有某種錯誤產生。匹配正則表達式一旦用 r e g c o m p ( ) 函數成功地編譯了正則表達式,接下來就可以調用 r e g e x e c ( ) 函數完成模式匹配: i n t r e g e x e c ( c o n s t r e g e x _ t * p r e g , c o n s t c h a r * s t r i n g , s i z e _ t n m a t c h , r e g m a t c h _ t p m a t c h [ ] , i n t e f l a g s ) ; t y p e d e f s t r u c t { r e g o f f _ t r m _ s o ; r e g o f f _ t r m _ e o ; } r e g m a t c h _ t ; 參數 p r e g 指向編譯后的正則表達式,參數 s t r i n g 是將要進行匹配的字符串,而參數 n m a t c h 和 p m a t c h 則用于把匹配結果返回給調用程序,最后一個參數 e f l a g s 決定了匹配的細節(jié)。在調用函數 r e g e x e c ( ) 進行模式匹配的過程中,可能在字符串 s t r i n g 中會有多處與給定的正則表達式相匹配,參數 p m a t c h 就是用來保存這些匹配位置的,而參數 n m a t c h 則告訴函數 r e g e x e c ( ) 最多可以把多少個匹配結果填充到 p m a t c h 數組中。當 r e g e x e c ( ) 函數成功返回時,從 s t r i n g + p m a t c h [ 0 ] . r m _ s o 到 s t r i n g + p m a t c h [ 0 ] . r m _ e o 是第一個匹配的字符串,而從 s t r i n g + p m a t c h [ 1 ] . r m _ s o 到 s t r i n g + p m a t c h [ 1 ] . r m _ e o ,則是第二個匹配的字符串,依此類推。釋放正則表達式無論什么時候,當不再需要已經編譯過的正則表達式時,都應該調用函數 r e g f r e e ( ) 將其釋放,以免產生內存泄漏。 v o i d r e g f r e e ( r e g e x _ t * p r e g ) ; 函數 r e g f r e e ( ) 不會返回任何結果,它僅接收一個指向 r e g e x _ t 數據類型的指針,這是之前調用 r e g c o m p ( ) 函數所得到的編譯結果。如果在程序中針對同一個 r e g e x _ t 結構調用了多次 r e g c o m p ( ) 函數, P O S I X 標準并沒有規(guī)定是否每次都必須調用 r e g f r e e ( ) 函數進行釋放,但建議每次調用 r e g c o m p ( ) 函數對正則表達式進行編譯后都調用一次 r e g f r e e ( ) 函數,以盡早釋放占用的存儲空間。報告錯誤信息如果調用函數 r e g c o m p ( ) 或 r e g e x e c ( ) 得到的是一個非 0 的返回值,則表明在對正則表達式的處理過程中出現了某種錯誤,此時可以通過調用函數 r e g e r r o r ( ) 得到詳細的錯誤信息。 s i z e _ t r e g e r r o r ( i n t e r r c o d e , c o n s t r e g e x _ t * p r e g , c h a r * e r r b u f , s i z e _ t e r r b u f _ s i z e ) ; 參數 e r r c o d e 是來自函數 r e g c o m p ( ) 或 r e g e x e c ( ) 的錯誤代碼,而參數 p r e g 則是由函數 r e g c o m p ( ) 得到的編譯結果,其目的是把格式化消息所必須的上下文提供給 r e g e r r o r ( ) 函數。在執(zhí)行函數 r e g e r r o r ( ) 時,將按照參數 e r r b u f _ s i z e 指明的最大字節(jié)數,在 e r r b u f 緩沖區(qū)中填入格式化后的錯誤信息,同時返回錯誤信息的長度。應用正則表達式最后給出一個具體的實例,介紹如何在 C 語言程序中處理正則表達式。 # i n c l u d e s t d i o . h ; # i n c l u d e s y s / t y p e s . h ; # i n c l u d e r e g e x . h ; / * 取子串的函數 * / s t a t i c c h a r * s u b s t r ( c o n s t c h a r * s t r , u n s i g n e d s t a r t , u n s i g n e d e n d ) { u n s i g n e d n = e n d - s t a r t ; s t a t i c c h a r s t b u f [ 2 5 6 ] ; s t r n c p y ( s t b u f , s t r + s t a r t , n ) ; s t b u f [ n ] = 0 ; r e t u r n s t b u f ; } / * 主程序 * / i n t m a i n ( i n t a r g c , c h a r * * a r g v ) { c h a r * p a t t e r n ; i n t x , z , l n o = 0 , c f l a g s = 0 ; c h a r e b u f [ 1 2 8 ] , l b u f [ 2 5 6 ] ; r e g e x _ t r e g ; r e g m a t c h _ t p m [ 1 0 ] ; c o n s t s i z e _ t n m a t c h = 1 0 ; / * 編譯正則表達式 * / p a t t e r n = a r g v [ 1 ] ; z = r e g c o m p ( r e g , p a t t e r n , c f l a g s ) ; i f ( z ! = 0 ) { r e g e r r o r ( z , r e g , e b u f , s i z e o f ( e b u f ) ) ; f p r i n t f ( s t d e r r , " % s : p a t t e r n ' % s ' \ n " , e b u f , p a t t e r n ) ; r e t u r n 1 ; } / * 逐行處理輸入的數據 * / w h i l e ( f g e t s ( l b u f , s i z e o f ( l b u f ) , s t d i n ) ) { + + l n o ; i f ( ( z = s t r l e n ( l b u f ) ) ; 0 l b u f [ z - 1 ] = = ' \ n ' ) l b u f [ z - 1 ] = 0 ; / * 對每一行應用正則表達式進行匹配 * / z = r e g e x e c ( r e g , l b u f , n m a t c h , p m , 0 ) ; i f ( z = = R E G _ N O M A T C H ) c o n t i n u e ; e l s e i f ( z ! = 0 ) { r e g e r r o r ( z , r e g , e b u f , s i z e o f ( e b u f ) ) ; f p r i n t f ( s t d e r r , " % s : r e g c o m ( ' % s ' ) \ n " , e b u f , l b u f ) ; r e t u r n 2 ; } / * 輸出處理結果 * / f o r ( x = 0 ; x n m a t c h p m [ x ] . r m _ s o ! = - 1 ; + + x ) { i f ( ! x ) p r i n t f ( " % 0 4 d : % s \ n " , l n o , l b u f ) ; p r i n t f ( " $ % d = ' % s ' \ n " , x , s u b s t r ( l b u f , p m [ x ] . r m _ s o , p m [ x ] . r m _ e o ) ) ; } } / * 釋放正則表達式 * / r e g f r e e ( r e g ) ; r e t u r n 0 ; } 上述程序負責從命令行獲取正則表達式,然后將其運用于從標準輸入得到的每行數據,并打印出匹配結果。執(zhí)行下面的命令可以編譯并執(zhí)行該程序: # g c c r e g e x p . c - o r e g e x p # . / r e g e x p ' r e g e x [ a - z ] * ' r e g e x p . c 0 0 0 3 : # i n c l u d e r e g e x . h ; $ 0 = ' r e g e x ' 0 0 2 7 : r e g e x _ t r e g ; $ 0 = ' r e g e x ' 0 0 5 4 : z = r e g e x e c ( r e g , l b u f , n m a t c h , p m , 0 ) ; $ 0 = ' r e g e x e c ' 小結對那些需要進行復雜數據處理的程序來說,正則表達式無疑是一個非常有用的工具。本文重點在于闡述如何在 C 語言中利用正則表達式來簡化字符串處理,以便在數據處理方面能夠獲得與 P e r l 語言類似的靈活性。

        如何用正則表達式匹配到C語言中的函數實現部分的函數頭部分。

        一些補充:腳本是ruby,領會精神就行了……

        /regexp/m 多行模式正則表達式

        /regexp/ 單行模式正則表達式

        =~ 若匹配返回第一個匹配的位置,不匹配則返回nil

        $1,$2 反向引用(分別對應第1,2個括號)

        string[start...end] 截取字符串從start到end-1的那段dup 復制sub,gsub,sub!,gsub! 字符串替換,感嘆號表示替換自身,沒感嘆號則返回新串正則表達式語法全世界都大同小異,就不解釋了……

        用C語言完成一個正則表達式的匹配: 字符串中只有*和?是可變字符且位置和個數不固定,其他的字符位置固定

        #includestdio.h

        #includestring.h

        #include stdlib.h

        //1、'?'很好處理,只要在原有的定位函數中加一點點就行:

        int index(char *s,char *t,int pos)

        {

        int i=pos,j=0,lens=strlen(s),lent=strlen(t);

        while(ilensjlent)

        {

        if(s[i]==t[j]||t[j]=='?')

        {

        ++i;

        ++j;

        }

        else

        {

        i=i-j+1;

        j=0;

        }

        }

        if(j==lent)return i-lent;

        else return -1;

        }

        /*2、'*'的處理有些麻煩,很自然的想法是'*'把整個T串分成若干不含'*'的子串,

        拿這些子串依次匹配S串。

        按這樣的方法可以把S串分成兩類:

        A、T=T1*T2*...Tn*,其中Ti為不含'*'的子串,且不為空(T1可為空)。

        B、T=T1*T2*...Tn

        二者的差別只在于尾部是否有'*'。

        拿T匹配S,

        首先 T1匹配S頭部,index(s,t1,0)==0

        然后 用循環(huán)完成后面的匹配,從前一次匹配后的末尾位置開始向后匹配,

        如果匹配成功再把末尾位置記錄下來。這里只用了最左匹配,為什么就足夠了呢?

        比如實際中的情況T1串可能在S串不止出現一次,為什么只考慮最左一個呢?

        因為整個匹配過程是從左向右,最左匹配可以保證余下的S子中最長,更有利于后面T子串的匹配成功,

        試想如果T1最左匹配不成功,靠右一些有可能成功嗎?

        例:T="*is*a*" S="this is a program"

        T 子串"is"在S中有出現兩個位置,匹配的時候只需要考慮最左邊那個"is"就行了,

        因為最左邊的"is"匹配成功后,余下的S子串是" is a program",余下的T子串是"*a",

        最左匹配可使余下的S子串最長,匹配的可能最大,最容易匹配的情況已經驗證了,

        就不用再做無用功了。

        */

        int match(char *s,char *t)

        {

        int i=0,j=0,lens=strlen(s),lent=strlen(t);

        char buf[128];

        int bufp=0;

        while(jlentt[j]!='*')

        {

        buf[bufp]=t[j];

        ++bufp;++j;

        }

        buf[bufp]='\0';

        if(index(s,buf,0)!=0)return 0;

        i=bufp;

        while(1)

        {

        while(jlentt[j]=='*')++j;

        if(j==lent)return 1;

        bufp=0;

        while(jlentt[j]!='*')

        {

        buf[bufp]=t[j];

        ++bufp;++j;

        }

        buf[bufp]='\0';

        if(j==lent)

        {

        if(index(s,buf,i)!=lens-bufp)return 0;

        return 1;

        }

        if((i=index(s,buf,i))==-1)return 0;

        i+=bufp;

        }

        }

        void main()

        {

        char s[128];

        char t[128]="mad*se$?243*.xml";

        memset(s,'\0',128);//初始化s

        printf("輸入字符串,進行匹配\n");

        gets(s);

        if(match(s,t))

        printf("匹配\n");

        else

        printf("不匹配\n");

        }

          • <bdo id='v6so9cgh'></bdo><ul id='wqycug6f'></ul>
          • <legend id='3g4ioono'><style id='byne3n99'><dir id='3mtrm6ps'><q id='w76m00tc'></q></dir></style></legend>

            <i id='xrkgj5yl'><tr id='py30tk0s'><dt id='xx9k6qyo'><q id='7f38h7h0'><span id='wriugwa5'><b id='gtuw4nc2'><form id='t4ot1kuj'><ins id='ftc341wd'></ins><ul id='haevf7z1'></ul><sub id='zq7k463x'></sub></form><legend id='wl6t6jtp'></legend><bdo id='95zgf293'><pre id='r47ukg2a'><center id='q1n4o7q1'></center></pre></bdo></b><th id='2x5xaacp'></th></span></q></dt></tr></i><div class="l4l4wckh0sl" id='xmlxnhd3'><tfoot id='y4sndocf'></tfoot><dl id='maho14vx'><fieldset id='vco9moot'></fieldset></dl></div>
          • <tfoot id='43evtznz'></tfoot>

              <small id='g0r58ute'></small><noframes id='h8ghwdkc'>

                    <tbody id='6iy47wp3'></tbody>
                • 上一篇:func函數+在C語言 func函數在c語言中

                  欄    目:C語言

                  下一篇:c語言函數調用后清空內存 c語言調用函數刪除字符

                  本文標題:c語言的正則匹配函數 c語言正則表達式函數庫

                  本文地址:http://www.jygsgssxh.com/a1/Cyuyan/17107.html

                  網頁制作CMS教程網絡編程軟件編程腳本語言數據庫服務器

                  如果侵犯了您的權利,請與我們聯系,我們將在24小時內進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

                  聯系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

                  Copyright © 2002-2020 腳本教程網 版權所有

                • <legend id='2zrjca35'><style id='ifpqebot'><dir id='9nd2rx17'><q id='powxsflz'></q></dir></style></legend>
                    <bdo id='pma608k4'></bdo><ul id='0epe88uq'></ul>

                  <small id='d3bxmu8x'></small><noframes id='xoxxwhbv'>

                      <tfoot id='cnanhyhe'></tfoot>
                      <i id='lp7sb479'><tr id='b6yhr3mj'><dt id='c4pbhrla'><q id='g9xghv7h'><span id='y0xchrss'><b id='3m2vkget'><form id='iih7vy19'><ins id='y2ofu7er'></ins><ul id='a1wanzjq'></ul><sub id='y5hssqop'></sub></form><legend id='fkb6w02y'></legend><bdo id='a61c6287'><pre id='e4ipwf00'><center id='60uta6ut'></center></pre></bdo></b><th id='byohd5dt'></th></span></q></dt></tr></i><div class="l4l4wckh0sl" id='j9pziy2h'><tfoot id='l6umpdya'></tfoot><dl id='s3f1few2'><fieldset id='mjcrk9pg'></fieldset></dl></div>