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);
}
}
:getsome: