天天看點

在日常工作中使用正規表達式

        在我們寫代碼的時候,基本上都用不到正規表達式,除了要在我們的代碼中校驗郵箱和手機号碼的合法性的功能外。其實我們幾乎每天都會使用正規表達式,因為使用正規表達式不一定要用在代碼中,日常的查找替換都可以使用到。

        支援正規表達式的文本編輯器有很多,如notepad++,UE,Komodo Edit等。個人覺得Komode Edit對正規表達式的支援比UE和notepad++要強大,而且這個編輯器有windows版本和linux版本。

        下面有幾個簡單的例子:

         1.如果你定義了一個類,類裡面有很多成員變量,在類的構造函數中要給這些成員變量賦初值。

int attr1;
int attr2;
int attr3;
int attr4;
int attr5;
int attr6;
int attr7;
int attr8;
int attr9;
int attr10;
int attr11;
int attr12;
int attr13;
int attr14;
int attr15;
           

       打開Komode Edit進行查找和替換:

在日常工作中使用正規表達式

      替換後的代碼為:

attr1 = -1;
attr2 = -1;
attr3 = -1;
attr4 = -1;
attr5 = -1;
attr6 = -1;
attr7 = -1;
attr8 = -1;
attr9 = -1;
attr10 = -1;
attr11 = -1;
attr12 = -1;
attr13 = -1;
attr14 = -1;
attr15 = -1;
           

       2.如果你在寫一個Oracle存儲過程,從資料表中擷取記錄,并儲存到一個集合中

       假設A表和B表結構如下:

CREATE TABLE tableA
(tableid int not null,
col1 int not null,
col2  int not null,
col3   int not null,
col4 int not null,
col5  int not null,
col6   int not null,
col7 int not null,
col8  int not null,
col9   int not null,
col10 int not null,
col11  int not null,
col12   int not null,
col13 int not null,
col14  int not null,
col15   int not null
)

CREATE TABLE tableB
(tableBid int not null,
parentid int not null,
coll1 int not null,
coll2  int not null,
coll3   int not null,
coll4 int not null,
coll5  int not null,
coll6   int not null,
coll7 int not null,
coll8  int not null,
coll9   int not null,
coll10 int not null,
coll11  int not null,
coll12   int not null,
coll13 int not null,
coll14  int not null,
coll15   int not null
)
           

        假設我們的存儲過程大緻是下面的樣子,很長時間都沒有寫存儲過程,基本上都不知道怎麼寫了。

for cur in (
select a.*,b.* from tableA a, tableB b where a.tableid = b.parentid
) loop

p_nt_results.extend;
p_nt_results(i) = new ob_result;
...
end loop;
           

       省略号的部分就是我們要給集合指派的部分,這部分可以使用正規表達式來做。

       使用該語句

select a.*,b.* from tableA a, tableB b where a.tableid = b.parentid having 1 <> 1;
           

       得到一個空的記錄集,然後在切換到列模式下,如下圖所示:

在日常工作中使用正規表達式

         複制字段到Komodo Edit中進行查找替換。

在日常工作中使用正規表達式

      替換後的結果如下:

p_nt_results(i) .TABLEID = cur.TABLEID;
p_nt_results(i) .COL1 = cur.COL1;
p_nt_results(i) .COL2 = cur.COL2;
p_nt_results(i) .COL3 = cur.COL3;
p_nt_results(i) .COL4 = cur.COL4;
p_nt_results(i) .COL5 = cur.COL5;
p_nt_results(i) .COL6 = cur.COL6;
p_nt_results(i) .COL7 = cur.COL7;
p_nt_results(i) .COL8 = cur.COL8;
p_nt_results(i) .COL9 = cur.COL9;
p_nt_results(i) .COL10 = cur.COL10;
p_nt_results(i) .COL11 = cur.COL11;
p_nt_results(i) .COL12 = cur.COL12;
p_nt_results(i) .COL13 = cur.COL13;
p_nt_results(i) .COL14 = cur.COL14;
p_nt_results(i) .COL15 = cur.COL15;
p_nt_results(i) .TABLEBID = cur.TABLEBID;
p_nt_results(i) .PARENTID = cur.PARENTID;
p_nt_results(i) .COLL1 = cur.COLL1;
p_nt_results(i) .COLL2 = cur.COLL2;
p_nt_results(i) .COLL3 = cur.COLL3;
p_nt_results(i) .COLL4 = cur.COLL4;
p_nt_results(i) .COLL5 = cur.COLL5;
p_nt_results(i) .COLL6 = cur.COLL6;
p_nt_results(i) .COLL7 = cur.COLL7;
p_nt_results(i) .COLL8 = cur.COLL8;
p_nt_results(i) .COLL9 = cur.COLL9;
p_nt_results(i) .COLL10 = cur.COLL10;
p_nt_results(i) .COLL11 = cur.COLL11;
p_nt_results(i) .COLL12 = cur.COLL12;
p_nt_results(i) .COLL13 = cur.COLL13;
p_nt_results(i) .COLL14 = cur.COLL14;
p_nt_results(i) .COLL15 = cur.COLL15;
           

        這些内容也就是我們省略号的那部分。

        看了上面這些,大家是不是覺得正規表達式很有用,是不是很強大。它可以在一定程度上提高我們的工作效率。

繼續閱讀