I have file names separated by colon :
This one is working as expected
String fileName = "test.pdf:test1.txt:test2.png:test3.jpg:test4.jpeg:test5.doc";
String ext = "pdf";
System.out.println(fileName.matches(".*\\b\\."+ext+":\\b.*"));
but when a matching file is at the end, above solution does not work
String fileName = "test1.txt:test2.png:test3.jpg:test4.jpeg:test5.doc:test.pdf";
What is the regex to achieve it?
Change the pattern to look for :
or the end $
:
".*\\." + ext + "(:|$).*"
(Also, I removed the unnecessary \\\\b
.)
You can use pattern and matcher.
Pattern pdfPattern = Pattern.compile("\\.pdf");
if(pdfPattern.matcher(fileName).find()){
System.out.println("Found PDF");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.