battle programmers alliance
Would you like to react to this message? Create an account in a few clicks or log in to continue.


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

 [java] new regex class

Go down 
AuthorMessage
kurosen
codding
codding
kurosen

Posts : 228
Join date : 2012-04-17

[java] new regex class Empty
PostSubject: [java] new regex class   [java] new regex class EmptyWed Oct 14, 2020 10:21 am

Code:
package chobit;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// returns expression of type theRegex from the string str2Check
public class RegexUtil {
    public String regexChecker(String theRegex, String str2Check) {
        Pattern checkRegex = Pattern.compile(theRegex);
        Matcher regexMatcher = checkRegex.matcher(str2Check);
        while (regexMatcher.find()) {
            if (regexMatcher.group().length() != 0) {
                return regexMatcher.group().trim();
            }
        }
        return "";
    }

   public ArrayList<String> regexChecker2(String theRegex, String str2Check) {
      // return a list of all matches
      ArrayList<String> list = new ArrayList<String>();
      Pattern checkRegex = Pattern.compile(theRegex);
      Matcher regexMatcher = checkRegex.matcher(str2Check);
      while (regexMatcher.find()) {
         if (regexMatcher.group().length() != 0) {
            list.add(regexMatcher.group().trim());
         }
      }
      return list;
   }

   public String contactRegex(String str2Check) {
      // return a list of all matches
      String theRegex = "(?<=contact)(.*)";
      ArrayList<String> list = new ArrayList<String>();
      Pattern checkRegex = Pattern.compile(theRegex);
      Matcher regexMatcher = checkRegex.matcher(str2Check);
      while (regexMatcher.find()) {
         if (regexMatcher.group().length() != 0) {
            return regexMatcher.group().trim();
         }
      }
      return "";
   }

   public String emailRegex(String str2Check) {
      // return a list of all matches
      String theRegex = "^([_a-zA-Z0-9-]+(\\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*(\\.[a-zA-Z]{1,6}))?$";
      ArrayList<String> list = new ArrayList<String>();
      Pattern checkRegex = Pattern.compile(theRegex);
      Matcher regexMatcher = checkRegex.matcher(str2Check);
      while (regexMatcher.find()) {
         if (regexMatcher.group().length() != 0) {
            return regexMatcher.group().trim();
         }
      }
      return "";
   }

   public String duplicateRegex(String str2Check) {
      // return a list of all matches
      // String theRegex = "\\b(\\w+)(\\b\\W+\\b\\1\\b)*";
      String theRegex = "\\b([\\w\\s']+) \\1\\b"; // set to 1 repeat of a word like hadoken hadoken
      ArrayList<String> list = new ArrayList<String>();
      Pattern checkRegex = Pattern.compile(theRegex);
      Matcher regexMatcher = checkRegex.matcher(str2Check);
      while (regexMatcher.find()) {
         if (regexMatcher.group().length() != 0) {
            return uniqueWord(regexMatcher.group().trim());
         }
      }
      return "";
   }

   public String uniqueWord(String str) {
      ArrayList<String> list = new ArrayList<String>();
      String s[] = str.split(" ");

      String p = s[0];
      list.add(p);

      for (int i = 1; i < s.length; i++) {

         if (!(p == s[i])) {
            list.add(s[i]);
         }
         p = s[i];
      } // i

      return list.get(0);
   }
   public String afterWord(String word, String str2Check) {
      // return a list of all matches
      String theRegex = "(?<=" + word + ")(.*)";
      ArrayList<String> list = new ArrayList<String>();
      Pattern checkRegex = Pattern.compile(theRegex);
      Matcher regexMatcher = checkRegex.matcher(str2Check);
      while (regexMatcher.find()) {
         if (regexMatcher.group().length() != 0) {
            return regexMatcher.group().trim();
         }
      }
      return "";
   }

   public String phoneRegex1(String str2Check) {
      return regexChecker("[0]\\d{2}\\d{4}\\d{3}$", str2Check);
   }
}

get some
Back to top Go down
 
[java] new regex class
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: