Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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\\.])", "*"));

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You may simply match on \d+, which matches groups of numbers of one or more digits:

String ss = "ramesh_gone_to_avbp_92_vc.pdf";
String output = ss.replaceAll("\\d+", "*");
System.out.println(output);  // ramesh_gone_to_avbp_*_vc.pdf
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading