默认扩展名是3个字节. String s= "hello.txtmail.jpgworld.log"; Matcher m = Pattern.compile("[^\\.]*\\.[a-z]{3}").matcher(s); while (m.find()) { System.out.println(m.group()); }
public class String1 { public static void main(String[] args) { String s="hello.txtmail.jpgworld.log"; String s1=s.substring(0,9); String s2=s.substring(9,17); String s3=s.substring(17,26); System.out.println("s1="+s1); System.out.println("s2="+s2); System.out.println("s3="+s3); } }
String s = "hello.txtmail.jpgworld.log"; Matcher m = Pattern.compile(".*?\\.\\w{0,3}").matcher(s); while (m.find()) { System.out.println(m.group()); }可以匹配后缀0-3个长度的文件名
如果后缀长度固定3个的话,可以进行分隔,否则没有办法的。public class Test { public static void main(String[] arg) { String str = "hello.txtmail.jpgworld.log"; String[] strs = str.split("(?<=\\.\\w{3})");
for(String s : strs) { System.out.println(s); } } }
然后不断地匹配 *.txt, 如果不行就去匹配*.jpg, 直到把这个列表匹配完。不过正则的方式太慢了。手工写吧,很好玩。
String s= "hello.txtmail.jpgworld.log";
Matcher m = Pattern.compile("[^\\.]*\\.[a-z]{3}").matcher(s);
while (m.find()) {
System.out.println(m.group());
}
{
public static void main(String[] args)
{
String s="hello.txtmail.jpgworld.log";
String s1=s.substring(0,9);
String s2=s.substring(9,17);
String s3=s.substring(17,26);
System.out.println("s1="+s1);
System.out.println("s2="+s2);
System.out.println("s3="+s3);
}
}
Matcher m = Pattern.compile(".*?\\.\\w{0,3}").matcher(s);
while (m.find()) {
System.out.println(m.group());
}可以匹配后缀0-3个长度的文件名
public static void main(String[] arg) {
String str = "hello.txtmail.jpgworld.log";
String[] strs = str.split("(?<=\\.\\w{3})");
for(String s : strs) {
System.out.println(s);
}
}
}
文件名
a.txt.jpg.log
这个也是一个合法的log文件名