Hi Guys,

 

 

I have a string "We have 7 tutorials for Java, 2 tutorials for Javascript and 1 tutorial for Oracle"

I need to replace the numbers based on the below condition.

 

if more then 5, replace with many

if less then 5, replace with a few

if it is 1, replace with "only one"

 

below is my code, I am missing the equating part to replace the numbers could any one of you please help me out fixing this.

 

 

private static String REGEX="(\\d+)";

  private static String INPUT="We have 7 tutorials for Java, 2 tutorials for Javascript and 1 tutorial for Oracle";

  //String pattern= "(.*)(\\d+)(.*)";

  private static String REPLACE = "replace with many";

  public static void main(String[] args) {

  // Create a Pattern object

       Pattern r = Pattern.compile(REGEX);

    // Now create matcher object.

       Matcher m = r.matcher(INPUT);

       //replace the value 7 by the replace string

//How to equate the (\\d+)  greater than a number  and use it the below code.

              INPUT = m.replaceAll(REPLACE);

       //Print the final Result;

        System.out.println(INPUT);

 

 

 

Thanks and Regards,

FacebookTwitterLinkedin
Pin It
Joomla Tutorials for Beginners