在日常工作中使用正则表达式

        在我们写代码的时候,基本上都用不到正则表达式,除了要在我们的代码中校验邮箱和手机号码的合法性的功能外。其实我们几乎每天都会使用正则表达式,因为使用正则表达式不一定要用在代码中,日常的查找替换都可以使用到。

        支持正则表达式的文本编辑器有很多,如notepad++UEKomodo Edit等。个人觉得Komode Edit对正则表达式的支持比UEnotepad++要强大,而且这个编辑器有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;

        这些内容也就是我们省略号的那部分。

        看了上面这些,大家是不是觉得正则表达式很有用,是不是很强大。它可以在一定程度上提高我们的工作效率。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值