适合提取这种类型数字的编号
Pattern pattern = Pattern.compile("^\\d+"); Matcher matcher = pattern.matcher(fileArray[i].getName()); while (matcher.find()) { name = matcher.group(); System.out.println(name); }
效果:
源码:
package zero.file.videoProject.util.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import zero.file.videoProject.bean.video; import zero.file.videoProject.mappers.untils.tonameMapper; import java.io.File; import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; @RunWith(SpringRunner.class) @SpringBootTest public class UserMapperTest { @Autowired public tonameMapper tonamejpg; @Test public void testInsert(){ Pattern pattern = Pattern.compile("(.+(?=\\.mp4))+"); // Pattern pattern1 = Pattern.compile("([\\d\\d\\d] +| [.+(?=\\.mp4)] + )"); // Pattern pattern1 = Pattern.compile("[\\d\\d\\d] | [.+(?=\\.mp4)]"); // Pattern pattern1 = Pattern.compile("(\\d\\d\\d) | (.+(?=\\.mp4))"); Pattern pattern2 = Pattern.compile("^\\d+"); File f = new File("C:\\Users\\17517\\Videos\\bilibili\\Java"); File[] fileArray = f.listFiles(); String name = null; String name2 = null; ArrayList<String> name1 = new ArrayList<String>(); video video = new video(); for (int i = 0; i <= fileArray.length - 1; i++) { Matcher matcher = pattern.matcher(fileArray[i].getName()); while (matcher.find()) { name = matcher.group(); System.out.println(name); Matcher matcher2 = pattern2.matcher(name); while (matcher2.find()) { name2 = matcher2.group(); System.out.println(name2); } video.setId(name); video.setCategory(name); video.setName(name); video.setLabel(name); // System.out.println(video); // System.out.println(name); // tonamejpg.insert(video); } // while (matcher1.find()) { name2 = matcher1.group(); System.out.println(name); System.out.println(name2); // video.setId(name); video.setCategory(name); video.setName(name); video.setLabel(name); System.out.println(video); tonamejpg.insert(video); // } } } }