简体   繁体   English

用正则表达式 java 替换字符串

[英]Replace a string with regex java

I want to replace a sw version from a string like this: " XSYS: 3.5.0 " with something like this: " XSYS: 3.10.0 ".我想用这样的字符串替换 sw 版本:“XSYS: 3.5.0”,如下所示:“XSYS:3.10.0”。 Only that i want a method that uses regex that can change whatever is after "XSYS: ", whether the are xx.yy.zz or x.yyy.zz characters after that or combinations like these.只是我想要一种使用正则表达式的方法,该方法可以更改“XSYS:”之后的任何内容,无论是 xx.yy.zz 还是 x.yyy.zz 之后的字符或类似的组合。 It has to be something generic, so that if the version will become eventualy 5.100.23 (it's just an example) it will keep working.它必须是通用的,这样如果版本最终变为 5.100.23(这只是一个示例),它将继续工作。 I don't know if I was right in phrasing.我不知道我的措辞是否正确。

I suggest you take a look at this site, as it helped me understand regex better: https://regex101.com/ else if a piece of code is what you want, please consider that i didn't test it:我建议你看看这个网站,因为它帮助我更好地理解正则表达式: https://regex101.com/否则如果你想要一段代码,请考虑我没有测试它:

String regex = "^XSYS: \\d{1,3}.\\d{1,3}.\\d{1,3}$"
Boolean matches = yourString.matches(regex);

Hope it helped:)希望它有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM