battle programming a forum for elite programmers with extreme will power to sharpen theire skills
 
HomeCalendarFAQSearchMemberlistUsergroupsRegisterLog in
Share
 

 java the regex say function

Go down 
AuthorMessage
Moti Barski

Moti Barski

Posts : 330
Join date : 2011-08-02

java the regex say function Empty
PostSubject: java the regex say function   java the regex say function EmptyWed Dec 19, 2018 8:14 am

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


🇵🇷

_________________
MB over and out emp it up
Back to top Go down
View user profile
 
java the regex say function
Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
battle programmers alliance :: battle programming alliance :: battle programming-
Jump to: