import java.util.regex.Matcher; import java.util.regex.Pattern; String s = "c:\test.txt"; //String regEx = ".+[\\]{0,}.+$"; String regEx = "\\"; Pattern pat = Pattern.compile(regEx); Matcher mat = pat.matcher(s); while(mat.find()){ System.out.println(mat.group()); }