Java tutorial
//package com.java2s; public class Main { private static final String FILE_NAME_PATTERN = "^(.+)\\.([^\\.]+)$"; private static String sanitizeFileName(String fileName) { StringBuilder sanitized = new StringBuilder(); sanitized.append(fileName.replaceAll(FILE_NAME_PATTERN, "$1").replaceAll("[^A-Za-z0-9_\\-]", "_")); sanitized.append(fileName.replaceAll(FILE_NAME_PATTERN, "$2")); return sanitized.toString(); } }