public static List<String> filterCommands(Collection<String> commands, String whitelistRegex,
      String blacklistRegex, Logger log) {
    List<String> filteredCommands = new LinkedList<String>();
    Pattern whitelistPattern = Pattern.compile(whitelistRegex);
    Pattern blacklistPattern = Pattern.compile(blacklistRegex);
    for (String command : commands) {
      if (whitelistPattern.matcher(command).matches()
          && !blacklistPattern.matcher(command).matches()) {
        filteredCommands.add(command);
      } else {
        log.warn(String.format("Removing restricted command: %s", command));
      }
    }
    return filteredCommands;
  }
public static List < String > filterCommands ( Collection < String > commands , String whitelistRegex , String blacklistRegex , Logger log ) { List < String > filteredCommands = new LinkedList < String > ( ) ; Pattern whitelistPattern = Pattern . compile ( whitelistRegex ) ; Pattern blacklistPattern = Pattern . compile ( blacklistRegex ) ; for ( String command : commands ) { if ( whitelistPattern . matcher ( command ) . matches ( ) && ! blacklistPattern . matcher ( command ) . matches ( ) ) { filteredCommands . add ( command ) ; } else { log . warn ( String . format ( "Removing restricted command: %s" , command ) ) ; } } return filteredCommands ; }
Filter a collection of String commands to match a whitelist regex and not match a blacklist
regex.

@param commands Collection of commands to be filtered
@param whitelistRegex whitelist regex to work as inclusion criteria
@param blacklistRegex blacklist regex to work as exclusion criteria
@param log logger to report violation
@return filtered list of matching. Empty list if no command match all the criteria.