我们通过 netbeans快捷键ctrl+f可以调出查找输入框,通过ctrl+h来调查替换输入框,例如替换输入框界面如下,如果要使用正则进行替换就要构想“正则表达式”一项。
我们通过举例来说明怎样使用:
现有一个HTML文档,其中多处的内容为:
1
2
3
4
5
6
|
|
1
2
3
4
5
6
|
|
NetBeans的查找、替换支持基于Java的正则表达式(详见Java Class Pattern),可以较好地解决此问题。
1. 在NetBeans中查找替换窗格中,将“正则表达式的”选项打勾,使其支持正则表达式。
2. 使用下面的模式匹配我们要替换的内容:
1
|
|
1
|
( |
4. 通过$1引用捕获组(及小括号中的内容),并直接 替换后面的内容:
1
|
$1style = "padding: 0.5em;" >
|
再例如,在批量修改一些html文件、css文件中的图片路径、背景图片路径的时候经常会用到正则去批量匹配然后替换,那么例如图片的路径正则我们可以写成下面形式:
例如替换成image目录下:
==========================
例如
要将程序中的:
GSMC = request.getParameter("GSMC").trim();
GSJC = request.getParameter("GSJC").trim();
City = request.getParameter("City").trim();
Address = request.getParameter("Address").trim();
ZipCode = request.getParameter("ZipCode").trim();
Telephone = request.getParameter("Telephone").trim();
Website = request.getParameter("Website").trim();
GSCode = request.getParameter("GSCode").trim();
GSJC = request.getParameter("GSJC").trim();
City = request.getParameter("City").trim();
Address = request.getParameter("Address").trim();
ZipCode = request.getParameter("ZipCode").trim();
Telephone = request.getParameter("Telephone").trim();
Website = request.getParameter("Website").trim();
GSCode = request.getParameter("GSCode").trim();
替换成如下代码:
GSMC = CommonUtil.trimWithNull(request.getParameter("GSMC"));
GSJC = CommonUtil.trimWithNull(request.getParameter("GSJC"));
City = CommonUtil.trimWithNull(request.getParameter("City"));
Address = CommonUtil.trimWithNull(request.getParameter("Address"));
ZipCode = CommonUtil.trimWithNull(request.getParameter("ZipCode"));
Telephone = CommonUtil.trimWithNull(request.getParameter("Telephone"));
Website = CommonUtil.trimWithNull(request.getParameter("Website"));
GSCode = CommonUtil.trimWithNull(request.getParameter("GSCode"));
GSJC = CommonUtil.trimWithNull(request.getParameter("GSJC"));
City = CommonUtil.trimWithNull(request.getParameter("City"));
Address = CommonUtil.trimWithNull(request.getParameter("Address"));
ZipCode = CommonUtil.trimWithNull(request.getParameter("ZipCode"));
Telephone = CommonUtil.trimWithNull(request.getParameter("Telephone"));
Website = CommonUtil.trimWithNull(request.getParameter("Website"));
GSCode = CommonUtil.trimWithNull(request.getParameter("GSCode"));
正则表达式替换如下:
Find: request.getParameter\("(\S*)"\)\.trim\(\)
replace: CommonUtil.trimWithNull\(request.getParameter\("$1"\)\)
x01, x02,x03,x04,x05,x06,x07, x08,x09,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,x21,x22,x23,x24,x25,x26,x27,x28,x29,x30,x31 替换成:
x01 as \"X01\", x02 as \"X02\",x03 as \"X03\",x04 as \"X04\",x05 as \"X05\",x06 as \"X06\",x07 as \"X07\", x08 as \"X08\",x09 as \"X09\",x10 as \"X10\",x11 as \"X11\",x12 as \"X12\",x13 as \"X13\",x14 as \"X14\",x15 as \"X15\",x16 as \"X16\",x17 as \"X17\",x18 as \"X18\",x19 as \"X19\",x20 as \"X20\",x21 as \"X21\",x22 as \"X22\",x23 as \"X23\",x24 as \"X24\",x25 as \"X25\",x26 as \"X26\",x27 as \"X27\",x28 as \"X28\",x29 as \"X29\",x30 as \"X30\",x31 as \"X31\"
find: x([\d]{2})
replace:x$1 as \\"X$1\\"