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();
  }
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 ( ) ; }
get all allowed characters which can be part of a String which matches a given regular
expression. TODO: this is a first feature incomplete implementation, has to be improved.

@param pregEx string contains a regular expression pattern
@return string with all characters that can be part of a string that matches the regex