日期:2014-05-20 浏览次数:20774 次
//供参考 import java.util.regex.Pattern; import java.util.regex.Matcher; import java.util.List; import java.util.ArrayList; import java.io.File; class Test { public static void main(String[] args) { //取名称 String str = "vvMall/mallStoreImg/122/htmlImg/7ca76e82-0270-48f8-bd79-87bf1cb1486c.jpg /><img /vvMall/mallStoreImg/122/htmlImg/81f2335e-eb5d-4a52-9a17-de04007495e6.jpg/>看看啦啦的萨<span style=background-color: rgb(255, 255, 102);>芬阿萨德安抚</span><br />"; String regex = "[\\w,-]+\\.jpg"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(str); List<String> list = new ArrayList<String>(); while(m.find()) { list.add(m.group()); } System.out.println(list); //取文件 File file = new File("d:\\1\\"); File[] files = file.listFiles(); for(File f :files) { if(f.isFile()) list.add(f.toString()); } System.out.println(list); } }
------解决方案--------------------
String regex = "[^/]+\\.jpg";
------解决方案--------------------
package com.dai.csdn; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PatternTest1 { public static void main(String[] args) { String str = "<img src=\"/vvMall/mallStoreImg/122/htmlImg/7ca76e82-0270-48f8-bd79-87bf1cb1486c.jpg\" alt=\"\" /><img src=\"/vvMall/mallStoreImg/122/htmlImg/81f2335e-eb5d-4a52-9a17-de04007495e6.jpg\" alt=\"\" />看看啦啦的萨<span style=\"background-color: rgb(255, 255, 102);\">芬阿萨德安抚</span><br />"; Pattern p = Pattern.compile("[\\w,-]+\\.jpg"); Matcher m = p.matcher(str); List<String> l = new ArrayList<String>(); while(m.find()) { l.add(m.group()); } for (int i = 0; i < l.size(); i++) { System.out.println(l.get(i)); } File file = new File("D://pro");//文件夹路径 List<String> names = new ArrayList<String>(); myListFiles(file, names); for (int i = 0; i < names.size(); i++) { System.out.println(names.get(i)); } } private static void myListFiles(File file, List<String> names) { if (file.isDirectory()) { File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { myListFiles(files[i],names); } } else { names.add(file.getName()); } } }