为了账号安全,请及时绑定邮箱和手机立即绑定

求大佬指点哈!求这个正则怎么写?

求大佬指点哈!求这个正则怎么写?

泛舟湖上清波郎朗 2019-06-10 09:35:33
比如这么一行字符串3"Freisasse"500"privileges/ps_03_freisasse.lua"22150|我想改变成3"Freisasse"50000"privileges/ps_03_freisasse.lua"22150|也即第三个数字X100,请问怎么写?谢谢,网上找到的例子都是简单的全部替换,这里面很多数字只要替换一个就行了。源文本1"Unfreier"0""000|2"Gemeiner"250"privileges/ps_02_gemeiner.lua"110|3"Freisasse"500"privileges/ps_03_freisasse.lua"220|4"Beisasse"2000"privileges/ps_04_beisasse.lua"240|5"Buerger"6000"privileges/ps_05_buerger.lua"370|6"Freibuerger"15000"privileges/ps_06_freibuerger.lua"3100|7"Patrizier"30000"privileges/ps_07_patrizier.lua"4150|8"Edelmann"80000"privileges/ps_08_edelmann.lua"4301|9"Freiherr"150000"privileges/ps_09_freiherr.lua"5451|10"Baron"300000"privileges/ps_10_baron.lua"5552|11"Graf"600000"privileges/ps_11_graf.lua"51002|12"Markgraf"1000000"privileges/ps_12_markgraf.lua"52003|13"Fuerst"1500000"privileges/ps_13_fuerst.lua"55004|14"Reichsfuerst"2000000"privileges/ps_14_reichsfuerst.lua"59995|放一下我的处理代码,privateStringSecondNumber(Strings,Strings2){booleanflag=false;booleanflag1=false;for(inti=0;i<s.length();i++){if(s.charAt(i)'9'){flag=true;}if(flag==true&&s.charAt(i)>='0'&&s.charAt(i)<='9'){flag1=true;}if(flag==true&&flag1==true&&(s.charAt(i)'9')){StringBuffersb=newStringBuffer(s);returnsb.insert(i,s2).toString();}}returnnull;}SecondNumber(s,"00");Sostupid!
查看完整描述

2 回答

?
HUX布斯

TA贡献1876条经验 获得超6个赞

看下是不是你想要的结果
publicstaticvoidmain(String[]args){
Stringstr="1\"Unfreier\"0\"\"000|\r\n2\"Gemeiner\"250\"privileges/ps_02_gemeiner.lua\"110|\r\n3\"Freisasse\"500\"privileges/ps_03_freisasse.lua\"220|\r\n4\"Beisasse\"2000\"privileges/ps_04_beisasse.lua\"240|\r\n5\"Buerger\"6000\"privileges/ps_05_buerger.lua\"370|\r\n6\"Freibuerger\"15000\"privileges/ps_06_freibuerger.lua\"3100|\r\n7\"Patrizier\"30000\"privileges/ps_07_patrizier.lua\"4150|\r\n8\"Edelmann\"80000\"privileges/ps_08_edelmann.lua\"4301|\r\n9\"Freiherr\"150000\"privileges/ps_09_freiherr.lua\"5451|\r\n10\"Baron\"300000\"privileges/ps_10_baron.lua\"5552|\r\n11\"Graf\"600000\"privileges/ps_11_graf.lua\"51002|\r\n12\"Markgraf\"1000000\"privileges/ps_12_markgraf.lua\"52003|\r\n13\"Fuerst\"1500000\"privileges/ps_13_fuerst.lua\"55004|\r\n14\"Reichsfuerst\"2000000\"privileges/ps_14_reichsfuerst.lua\"59995|";
//指定替换第1,2,3行的数字
Stringresult1=updatesNumByIndex(newint[]{1,2,3},str,"00");
//默认替换所有行的数字
Stringresult2=updatesNumByIndex(null,str,"00");
System.out.println(result1);
System.out.println(result2);
}
/**
*
*@paramindexs指定行下标
*@paraminput原字符串
*@paramreplement填充物
*@return
*/
publicstaticStringupdatesNumByIndex(int[]indexs,Stringinput,Stringreplement){
StringindexRegex="\\d{1,100000}";
if(indexs!=null&&indexs.length>0){
StringBuffergroups=newStringBuffer("(?:");
intlen=indexs.length;
for(inti=0;igroups.append(indexs[i]);
groups.append(i!=len-1?"|":")");
}
indexRegex=groups.toString();
}
Stringregex="(?m)(?<=^"+indexRegex+"\\s\"\\w{1,100000}\"\\s\\d{1,100000})\\b";
returninput.replaceAll(regex,replement);
}
                            
查看完整回答
反对 回复 2019-06-10
?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

Stringstr="3\"Freisasse\"500\"privileges/ps_03_freisasse.lua\"22150200";
Matcherm=Pattern.compile("[0-9]").matcher(str);
inti=0;
intindex;
intnum;
StringnumChar;
intlastIndex;
while(m.find()){
numChar=m.group();
lastIndex=str.indexOf(numChar);
if(i==1){//根据需要修改
index=str.indexOf(numChar,lastIndex+1);
num=Integer.valueOf(numChar);
num=num*100;//0*100还是0可以是任何操作数据逻辑
str=str.substring(0,index)+num+str.substring(index+1);
break;
}
i++;
}
System.out.println(str);
测试结果:
转化前:3"Freisasse"300"privileges/ps_03_freisasse.lua"22150200
转化后:3"Freisasse"30000"privileges/ps_03_freisasse.lua"2215020000
看看是否符合你的要求
                            
查看完整回答
反对 回复 2019-06-10
  • 2 回答
  • 0 关注
  • 188 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信