Code:

public static void say(String nanika) {
      int foo = 1;
      String myString = regexChecker("(\\d+)(?= times)", nanika);
      String toSay = regexChecker("(?<=say)(.*)(?=\\d)", nanika);
      if (myString != "") {
         foo = Integer.parseInt(myString);
      } else {
         toSay = regexChecker("(?<=say)(.*)", nanika);
      }
      for (int i = 0; i < foo; i++) {
         System.out.println(toSay);
      }
   }

   public static String regexChecker(String theRegex, String str2Check) {
      // You define your regular expression (REGEX) using Pattern
      Pattern checkRegex = Pattern.compile(theRegex);
      // Creates a Matcher object that searches the String for
      // anything that matches the REGEX
      Matcher regexMatcher = checkRegex.matcher(str2Check);
      // Cycle through the positive matches and print them to screen
      // Make sure string isn't empty and trim off any whitespace
      while (regexMatcher.find()) {
         if (regexMatcher.group().length() != 0) {
            return regexMatcher.group().trim();
//            // You can get the starting and ending indexs
//            System.out.println("Start Index: " + regexMatcher.start());
//            System.out.println("Start Index: " + regexMatcher.end());
         }
      }
      return "";
   }



input :

say("say hello 4 times");
say("say moti rulz");
say("say shouryuken 5 times");

output :

hello
hello
hello
hello
moti rulz
shouryuken
shouryuken
shouryuken
shouryuken
shouryuken


🇵🇷