String winPath = "C:\\Users\\f.laudanum\\Documents\\My-Directory\\ "; String cleanPath; /* REGEXP pattern: '\\\s*' with doubled backslashes */ Pattern pattern = Pattern.compile("\\\\\\s*$"); Matcher match = pattern.matcher(winPath); /* Call to find(0) triggers the pattern search from text's start*/ if (match.find(0)) { cleanPath = match.replaceAll(""); } else { cleanPath = winPath; }