Skip to content

Instantly share code, notes, and snippets.

@sjbodzo
Created November 13, 2013 19:57
Show Gist options
  • Save sjbodzo/7455337 to your computer and use it in GitHub Desktop.
Save sjbodzo/7455337 to your computer and use it in GitHub Desktop.
simple regex practice
class RegexUtility {
public static String[] matches(String pattern, String[] words) {
String s = pattern;
for (int i = 0; i < words.length; i++) {
if (!(words[i].matches(pattern))) words[i] = null;
}
return words;
}
}
class RegexTester {
public static void main(String... argsz) {
String pattern = "([\\w])([\\D])\\1([\\w])([\\w])\\2";
String[] words1 = new String[]{"9B99BB","ABABBB","abaAB","abaab","ABaab"};
//printing out matches for given pattern and Strings
StringBuilder bob = new StringBuilder();
bob.append("Matches: ");
for (String s : RegexUtility.matches(pattern,words1)) {
if (s != null) bob.append(s + ", ");
}
bob.deleteCharAt(bob.length()-2); //remove trailling ','
System.out.println(bob);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment