@Test public void testGetAllowedCharactersForRegEx() { for (final Entry<String, String> entry : RegExUtilTestCases.getAllowedCharactersForRegExCases() .entrySet()) { Assert.assertEquals("allowed characters for regex failed", entry.getValue(), RegExUtil.getAllowedCharactersForRegEx(entry.getKey())); } }
public static String getAllowedCharactersForRegEx(final String pregEx) { if (StringUtils.isEmpty(pregEx)) { return null; } final StringBuilder regExCheck = new StringBuilder(); final StringBuilder regExCheckOut = new StringBuilder(); boolean inSequence = false; boolean isNegativeSequence = false; boolean inSize = false; boolean isMasked = false; regExCheck.append("(["); for (final char character : pregEx.toCharArray()) { switch (character) { case '\\': if (isMasked || inSequence) { regExCheck.append(character); } if (!inSequence) { isMasked = !isMasked; } break; case '^': if (inSequence) { if (isMasked) { regExCheck.append(character); } else { isNegativeSequence = true; } } isMasked = false; break; case '$': case '*': case '+': case '?': case '|': if (isMasked || inSequence) { regExCheck.append(character); } isMasked = false; break; case '[': if (isMasked || inSequence) { regExCheck.append(character); } else { inSequence = true; isNegativeSequence = false; } isMasked = false; break; case ']': if (isMasked) { regExCheck.append(character); } else { inSequence = false; isNegativeSequence = false; } isMasked = false; break; case '{': if (isMasked || inSequence) { regExCheck.append(character); } else { inSize = true; } isMasked = false; break; case '}': if (isMasked || inSequence) { regExCheck.append(character); } else { inSize = false; } isMasked = false; break; case '(': if (isMasked || inSequence) { regExCheck.append(character); } isMasked = false; break; case ')': if (isMasked || inSequence) { regExCheck.append(character); } isMasked = false; break; default: if (inSize) { if (character != ',' && (character < '0' || character > '9')) { regExCheck.append(character); } } else if (!isNegativeSequence) { if (isMasked) { if (regExCheckOut.length() > 1) { regExCheckOut.append('|'); } regExCheckOut.append('\\'); regExCheckOut.append(character); } else { regExCheck.append(character); } } isMasked = false; break; } } if (regExCheck.length() < 3) { regExCheck.delete(1, regExCheck.length()); } else { regExCheck.append(']'); if (regExCheckOut.length() > 0) { regExCheck.append('|'); } } regExCheck.append(regExCheckOut); regExCheck.append(')'); final RegExp regEx = RegExp.compile(regExCheck.toString()); final StringBuilder result = new StringBuilder(); for (int count = Character.MIN_VALUE; count < Character.MAX_VALUE; count++) { if (regEx.exec(String.valueOf((char) count)) != null) { result.append((char) count); } } return result.toString(); }
RegExUtil { public static String getAllowedCharactersForRegEx(final String pregEx) { if (StringUtils.isEmpty(pregEx)) { return null; } final StringBuilder regExCheck = new StringBuilder(); final StringBuilder regExCheckOut = new StringBuilder(); boolean inSequence = false; boolean isNegativeSequence = false; boolean inSize = false; boolean isMasked = false; regExCheck.append("(["); for (final char character : pregEx.toCharArray()) { switch (character) { case '\\': if (isMasked || inSequence) { regExCheck.append(character); } if (!inSequence) { isMasked = !isMasked; } break; case '^': if (inSequence) { if (isMasked) { regExCheck.append(character); } else { isNegativeSequence = true; } } isMasked = false; break; case '$': case '*': case '+': case '?': case '|': if (isMasked || inSequence) { regExCheck.append(character); } isMasked = false; break; case '[': if (isMasked || inSequence) { regExCheck.append(character); } else { inSequence = true; isNegativeSequence = false; } isMasked = false; break; case ']': if (isMasked) { regExCheck.append(character); } else { inSequence = false; isNegativeSequence = false; } isMasked = false; break; case '{': if (isMasked || inSequence) { regExCheck.append(character); } else { inSize = true; } isMasked = false; break; case '}': if (isMasked || inSequence) { regExCheck.append(character); } else { inSize = false; } isMasked = false; break; case '(': if (isMasked || inSequence) { regExCheck.append(character); } isMasked = false; break; case ')': if (isMasked || inSequence) { regExCheck.append(character); } isMasked = false; break; default: if (inSize) { if (character != ',' && (character < '0' || character > '9')) { regExCheck.append(character); } } else if (!isNegativeSequence) { if (isMasked) { if (regExCheckOut.length() > 1) { regExCheckOut.append('|'); } regExCheckOut.append('\\'); regExCheckOut.append(character); } else { regExCheck.append(character); } } isMasked = false; break; } } if (regExCheck.length() < 3) { regExCheck.delete(1, regExCheck.length()); } else { regExCheck.append(']'); if (regExCheckOut.length() > 0) { regExCheck.append('|'); } } regExCheck.append(regExCheckOut); regExCheck.append(')'); final RegExp regEx = RegExp.compile(regExCheck.toString()); final StringBuilder result = new StringBuilder(); for (int count = Character.MIN_VALUE; count < Character.MAX_VALUE; count++) { if (regEx.exec(String.valueOf((char) count)) != null) { result.append((char) count); } } return result.toString(); } }
RegExUtil { public static String getAllowedCharactersForRegEx(final String pregEx) { if (StringUtils.isEmpty(pregEx)) { return null; } final StringBuilder regExCheck = new StringBuilder(); final StringBuilder regExCheckOut = new StringBuilder(); boolean inSequence = false; boolean isNegativeSequence = false; boolean inSize = false; boolean isMasked = false; regExCheck.append("(["); for (final char character : pregEx.toCharArray()) { switch (character) { case '\\': if (isMasked || inSequence) { regExCheck.append(character); } if (!inSequence) { isMasked = !isMasked; } break; case '^': if (inSequence) { if (isMasked) { regExCheck.append(character); } else { isNegativeSequence = true; } } isMasked = false; break; case '$': case '*': case '+': case '?': case '|': if (isMasked || inSequence) { regExCheck.append(character); } isMasked = false; break; case '[': if (isMasked || inSequence) { regExCheck.append(character); } else { inSequence = true; isNegativeSequence = false; } isMasked = false; break; case ']': if (isMasked) { regExCheck.append(character); } else { inSequence = false; isNegativeSequence = false; } isMasked = false; break; case '{': if (isMasked || inSequence) { regExCheck.append(character); } else { inSize = true; } isMasked = false; break; case '}': if (isMasked || inSequence) { regExCheck.append(character); } else { inSize = false; } isMasked = false; break; case '(': if (isMasked || inSequence) { regExCheck.append(character); } isMasked = false; break; case ')': if (isMasked || inSequence) { regExCheck.append(character); } isMasked = false; break; default: if (inSize) { if (character != ',' && (character < '0' || character > '9')) { regExCheck.append(character); } } else if (!isNegativeSequence) { if (isMasked) { if (regExCheckOut.length() > 1) { regExCheckOut.append('|'); } regExCheckOut.append('\\'); regExCheckOut.append(character); } else { regExCheck.append(character); } } isMasked = false; break; } } if (regExCheck.length() < 3) { regExCheck.delete(1, regExCheck.length()); } else { regExCheck.append(']'); if (regExCheckOut.length() > 0) { regExCheck.append('|'); } } regExCheck.append(regExCheckOut); regExCheck.append(')'); final RegExp regEx = RegExp.compile(regExCheck.toString()); final StringBuilder result = new StringBuilder(); for (int count = Character.MIN_VALUE; count < Character.MAX_VALUE; count++) { if (regEx.exec(String.valueOf((char) count)) != null) { result.append((char) count); } } return result.toString(); }  }
RegExUtil { public static String getAllowedCharactersForRegEx(final String pregEx) { if (StringUtils.isEmpty(pregEx)) { return null; } final StringBuilder regExCheck = new StringBuilder(); final StringBuilder regExCheckOut = new StringBuilder(); boolean inSequence = false; boolean isNegativeSequence = false; boolean inSize = false; boolean isMasked = false; regExCheck.append("(["); for (final char character : pregEx.toCharArray()) { switch (character) { case '\\': if (isMasked || inSequence) { regExCheck.append(character); } if (!inSequence) { isMasked = !isMasked; } break; case '^': if (inSequence) { if (isMasked) { regExCheck.append(character); } else { isNegativeSequence = true; } } isMasked = false; break; case '$': case '*': case '+': case '?': case '|': if (isMasked || inSequence) { regExCheck.append(character); } isMasked = false; break; case '[': if (isMasked || inSequence) { regExCheck.append(character); } else { inSequence = true; isNegativeSequence = false; } isMasked = false; break; case ']': if (isMasked) { regExCheck.append(character); } else { inSequence = false; isNegativeSequence = false; } isMasked = false; break; case '{': if (isMasked || inSequence) { regExCheck.append(character); } else { inSize = true; } isMasked = false; break; case '}': if (isMasked || inSequence) { regExCheck.append(character); } else { inSize = false; } isMasked = false; break; case '(': if (isMasked || inSequence) { regExCheck.append(character); } isMasked = false; break; case ')': if (isMasked || inSequence) { regExCheck.append(character); } isMasked = false; break; default: if (inSize) { if (character != ',' && (character < '0' || character > '9')) { regExCheck.append(character); } } else if (!isNegativeSequence) { if (isMasked) { if (regExCheckOut.length() > 1) { regExCheckOut.append('|'); } regExCheckOut.append('\\'); regExCheckOut.append(character); } else { regExCheck.append(character); } } isMasked = false; break; } } if (regExCheck.length() < 3) { regExCheck.delete(1, regExCheck.length()); } else { regExCheck.append(']'); if (regExCheckOut.length() > 0) { regExCheck.append('|'); } } regExCheck.append(regExCheckOut); regExCheck.append(')'); final RegExp regEx = RegExp.compile(regExCheck.toString()); final StringBuilder result = new StringBuilder(); for (int count = Character.MIN_VALUE; count < Character.MAX_VALUE; count++) { if (regEx.exec(String.valueOf((char) count)) != null) { result.append((char) count); } } return result.toString(); }  static String getAllowedCharactersForRegEx(final String pregEx); }
RegExUtil { public static String getAllowedCharactersForRegEx(final String pregEx) { if (StringUtils.isEmpty(pregEx)) { return null; } final StringBuilder regExCheck = new StringBuilder(); final StringBuilder regExCheckOut = new StringBuilder(); boolean inSequence = false; boolean isNegativeSequence = false; boolean inSize = false; boolean isMasked = false; regExCheck.append("(["); for (final char character : pregEx.toCharArray()) { switch (character) { case '\\': if (isMasked || inSequence) { regExCheck.append(character); } if (!inSequence) { isMasked = !isMasked; } break; case '^': if (inSequence) { if (isMasked) { regExCheck.append(character); } else { isNegativeSequence = true; } } isMasked = false; break; case '$': case '*': case '+': case '?': case '|': if (isMasked || inSequence) { regExCheck.append(character); } isMasked = false; break; case '[': if (isMasked || inSequence) { regExCheck.append(character); } else { inSequence = true; isNegativeSequence = false; } isMasked = false; break; case ']': if (isMasked) { regExCheck.append(character); } else { inSequence = false; isNegativeSequence = false; } isMasked = false; break; case '{': if (isMasked || inSequence) { regExCheck.append(character); } else { inSize = true; } isMasked = false; break; case '}': if (isMasked || inSequence) { regExCheck.append(character); } else { inSize = false; } isMasked = false; break; case '(': if (isMasked || inSequence) { regExCheck.append(character); } isMasked = false; break; case ')': if (isMasked || inSequence) { regExCheck.append(character); } isMasked = false; break; default: if (inSize) { if (character != ',' && (character < '0' || character > '9')) { regExCheck.append(character); } } else if (!isNegativeSequence) { if (isMasked) { if (regExCheckOut.length() > 1) { regExCheckOut.append('|'); } regExCheckOut.append('\\'); regExCheckOut.append(character); } else { regExCheck.append(character); } } isMasked = false; break; } } if (regExCheck.length() < 3) { regExCheck.delete(1, regExCheck.length()); } else { regExCheck.append(']'); if (regExCheckOut.length() > 0) { regExCheck.append('|'); } } regExCheck.append(regExCheckOut); regExCheck.append(')'); final RegExp regEx = RegExp.compile(regExCheck.toString()); final StringBuilder result = new StringBuilder(); for (int count = Character.MIN_VALUE; count < Character.MAX_VALUE; count++) { if (regEx.exec(String.valueOf((char) count)) != null) { result.append((char) count); } } return result.toString(); }  static String getAllowedCharactersForRegEx(final String pregEx);  }