Created
November 13, 2013 19:57
-
-
Save sjbodzo/7455337 to your computer and use it in GitHub Desktop.
simple regex practice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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