battle programminga forum for elite programmerswith extreme will powerto sharpen theire skills
 
HomeCalendarFAQSearchMemberlistUsergroupsRegisterLog in
Share | 
 

 java the regex say function

Go down 
AuthorMessage
Moti Barski

avatar

Posts : 259
Join date : 2011-08-02

PostSubject: java the regex say function   Wed 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


pickle rick !

_________________
MB over and out _______________________     
Back to top Go down
View user profile
 
java the regex say function
Back to top 
Page 1 of 1
 Similar topics
-
» Verify command when used with Java,JUNIT and Eclipse
» LocalFileDetector (uploading files) - InternetExplorer Driver (Java platform)
» Exception in thread "main" java.net.BindException: Selenium is already running on port 4444. Or some other service is.
» How to compare images selenium, using java
» How to embed webdriver browser into java GUI/ attach to exsisting browser

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