正则表达式使用例子

 

》》分析下面的数字对,中间用逗号隔开:

asf123,343asdf
af1234  ,555asdf
as5556,   88fsda
666     ,    9999
dfd666ff,ffff9999fdd
dfd666  ,ffff9999fdd

 代码:

	String dataText = "asf123,343asdf/r/n    af1234  ,555asdf/r/n s5556,   88fsda/r/n 666     ,    9999/r/n dfd666ff,ffff9999fdd/r/n dfd666  ,ffff9999fdd";
        Pattern p = Pattern.compile("(\\d+)[ \t]*,[ \t]*(\\d+)");
        Matcher m = p.matcher(dataText);
        while (m.find()) {
            System.out.println(StringUtil.toInt(m.group(1)) + "," + StringUtil.toInt(m.group(2)));
        }

 结果:

666,9999

 

>>替换Java代码中的aaa.toString()到StrUtil.toStr(aaa)

				dayVo = new DayVo(dSales.toString(), dGm.toString(), dNgm
						.toString(), dOplgm.toString(), dTgm.toString());
				wTDVo = new WTDVo(wSales.toString(), wGm.toString(), wNgm
						.toString(), wOplgm.toString(), wTgm.toString());
				mTDVo = new MTDVo(mSales.toString(), aveMSales.toString(), mGm
						.toString(), mNgm.toString(), mOplgm.toString(), mTgm
						.toString());

 查找aaa.toString()正则表达式

(\w+)\s*.toString\(

 替换StrUtil.toStr(aaa)正则表达式

StrUtil.toStr\(\1

这里通过\1拿第一个捕获组,如果有多个,则使用\2,\3.....来取。

替换后的代码:

	dayVo = new DayVo(StrUtil.toStr(dSales), StrUtil.toStr(dGm), StrUtil.toStr(dNgm), StrUtil.toStr(dOplgm), StrUtil.toStr(dTgm));
        wTDVo = new WTDVo(StrUtil.toStr(wSales), StrUtil.toStr(wGm), StrUtil.toStr(wNgm), StrUtil.toStr(wOplgm), StrUtil.toStr(wTgm));
        mTDVo = new MTDVo(StrUtil.toStr(mSales), StrUtil.toStr(aveMSales), StrUtil.toStr(mGm), StrUtil.toStr(mNgm), StrUtil.toStr(mOplgm), StrUtil.toStr(mTgm));

 

 

 

 

你可能感兴趣的:(正则表达式)