英文:
Replace All numeric substring with character
问题
I have to replace all digit patterns with some character. For 1 digit its working fine for e.g for given string ramesh_gone_to_avbp_9_vc.pdf
its working fine changing it to ramesh_gone_to_avbp_*_vc.pdf
but for given input ramesh_gone_to_avbp_91_vc.pdf
its changing to ramesh_gone_to_avbp_**_vc.pdf
but i want output like ramesh_gone_to_avbp_*_vc.pdf
This is what i tried so far
String ss = "ramesh_gone_to_avbp_92_vc.pdf";
System.out.println(ss.replaceAll("(?<=[\\d\\.])-(?=[\\d\\.])", "*"));
英文:
I have to replace all digit patterns with some character. For 1 digit its working fine for e.g for given string ramesh_gone_to_avbp_9_vc.pdf
its working fine changing it to ramesh_gone_to_avbp_*_vc.pdf
but for given input ramesh_gone_to_avbp_91_vc.pdf
its changing to ramesh_gone_to_avbp_**_vc.pdf
but i want output like ramesh_gone_to_avbp_*_vc.pdf
This is what i tried so far
String ss = "ramesh_gone_to_avbp_92_vc.pdf";
System.out.println(ss.replaceAll("(?<=[\\d\\.])-(?=[\\d\\.])", "*"));
答案1
得分: 1
你可以简单地匹配\d+
,它匹配一个或多个数字组:
String ss = "ramesh_gone_to_avbp_92_vc.pdf";
String output = ss.replaceAll("\\d+", "*");
System.out.println(output); // ramesh_gone_to_avbp_*_vc.pdf
英文:
You may simply match on \d+
, which matches groups of numbers of one or more digits:
<!-- language: java -->
String ss = "ramesh_gone_to_avbp_92_vc.pdf";
String output = ss.replaceAll("\\d+", "*");
System.out.println(output); // ramesh_gone_to_avbp_*_vc.pdf
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论