Created
August 25, 2014 08:13
-
-
Save DarkSeraphim/7058ce0e9d2b56f39843 to your computer and use it in GitHub Desktop.
Regex filtering, gotta love it
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
| package net.darkseraphim.chat.filter; | |
| import org.apache.commons.lang.StringUtils; | |
| import java.util.regex.Pattern; | |
| /** | |
| * @author DarkSeraphim | |
| */ | |
| public class Regex | |
| { | |
| private final Pattern pattern; | |
| public Regex(String word) | |
| { | |
| StringBuilder regexBuilder = new StringBuilder(RegexConstants.getSpacing()); | |
| for(char c : word.toCharArray()) | |
| { | |
| regexBuilder.append(RegexConstants.getRegexReplacement(c)) | |
| .append(RegexConstants.getSpacing()); | |
| } | |
| regexBuilder.append(RegexConstants.getSpacing()); | |
| this.pattern = Pattern.compile(regexBuilder.toString(), Pattern.CASE_INSENSITIVE); | |
| } | |
| public boolean match(String word) | |
| { | |
| // Optional bypass for star usage | |
| //int starCount = 0; | |
| //for(char c : word.toCharArray()) | |
| // if(c == '*') starCount++; | |
| //if((double)starCount / (double)word.length() > 0.75) | |
| // return word; | |
| //System.out.println(this.pattern.pattern()); | |
| if(this.pattern.matcher(word).matches()) | |
| return true; | |
| return false; | |
| } | |
| public static void main(String[] args) | |
| { | |
| boolean f = new Regex("bitch").match("B1tch"); | |
| System.out.println(f); | |
| } | |
| } |
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
| package net.darkseraphim.chat.filter; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| /** | |
| * @author DarkSeraphim | |
| */ | |
| public class RegexConstants | |
| { | |
| private Map<Character, String> replacements = new HashMap<Character, String>(); | |
| { | |
| replacements.put('a', "[\\*|a|4|ä|à|ã|á|â|@|∂|ci|α|/\\\\|/\\-\\\\|\\^]"); | |
| replacements.put('b', "[\\*b86]"); | |
| replacements.put('c', "[\\*|c|z|k|\\(|\\<|\\[|¢|©|ς|\\{]"); | |
| replacements.put('d', "[\\*|d|[\\[\\{\\|]{1,2}]"); | |
| replacements.put('e', "[\\*eëéè3]"); | |
| replacements.put('f', "[\\*fv]"); | |
| replacements.put('g', "[\\*g9]"); | |
| replacements.put('h', "[\\*h]"); | |
| replacements.put('i', "[\\*i1ïíìî\\|!]"); | |
| replacements.put('j', "[\\*j]"); | |
| replacements.put('k', "[\\*|k|\\|\\<|c]"); | |
| replacements.put('l', "[\\*l1!]"); | |
| replacements.put('m', "[\\*m]"); | |
| replacements.put('n', "[\\*nñ]"); | |
| replacements.put('o', "[\\*oöóòõØ0]"); | |
| replacements.put('p', "[\\*p]"); | |
| replacements.put('q', "[\\*q]"); | |
| replacements.put('r', "[\\*r]"); | |
| replacements.put('s', "[\\*sz25\\$]"); | |
| replacements.put('t', "[\\*t7]"); | |
| replacements.put('u', "[\\*uüúùû]"); | |
| replacements.put('v', "[\\*vfw]"); | |
| replacements.put('w', "[\\*wv]"); | |
| replacements.put('x', "\\*|x|ks|iks"); | |
| replacements.put('y', "[\\*yÿý]"); | |
| replacements.put('z', "[\\*zs25\\$]"); | |
| } | |
| private static final RegexConstants instance = new RegexConstants(); | |
| public static String getRegexReplacement(char c) | |
| { | |
| return instance.replacements.containsKey(c) ? instance.replacements.get(c) : Character.toString(c); | |
| } | |
| public static String getSpacing() | |
| { | |
| return "[\\\\/\\{\\[\\. ,\\|\\-_!\\]\\}]*"; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment