Image File Extension Regular Expression Pattern.
| 
([^\s]+(\.(?i)(jpg|png|gif|bmp))$) | 
Description
| ( #Start of the group #1  [^\s]+                 #  must contains one or more anything (except white space)       (          #    start of the group #2         \.       #     follow by a dot "."         (?i)           #     ignore the case sensive checking for the following characters             (          #       start of the group #3              jpg #         contains characters "jpg"              |         #         ..or              png #         contains characters "png"              |         #         ..or              gif #         contains characters "gif"              |         #         ..or              bmp #         contains characters "bmp"             )          #       end of the group #3       )          #     end of the group #2        $               #  end of the string) #end of the group #1 | 
Whole
combination is means, must have 1 or more strings (but not white space), follow
by dot “.” and string end in “jpg” or “png” or “gif” or “bmp” , and the file
extensive is case-insensitive.
This regular
expression pattern is widely use in for different file extensive checking. You
can just change the end combination (jpg|png|gif|bmp) to come out
different file extension checking that suit your need.
Java Regular
Expression Example
| package com.mkyong.regex;  import java.util.regex.Matcher; import java.util.regex.Pattern;  public class ImageValidator{     private Pattern pattern;   private Matcher matcher;    private static final String IMAGE_PATTERN =                 "([^\\s]+(\\.(?i)(jpg|png|gif|bmp))$)";    public ImageValidator(){        pattern = Pattern.compile(IMAGE_PATTERN);   }    /**   * Validate image with regular expression   * @param image image for validation   * @return true valid image, false invalid image   */   public boolean validate(final String image){         matcher = pattern.matcher(image);        return matcher.matches();    }} | 
Image file that match:
1. “a.jpg”, “a.gif”,”a.png”, “a.bmp”,2. “..jpg”, “..gif”,”..png”, “..bmp”,
3. “a.JPG”, “a.GIF”,”a.PNG”, “a.BMP”,
4. “a.JpG”, “a.GiF”,”a.PnG”, “a.BmP”,
5. “jpg.jpg”, “gif.gif”,”png.png”, “bmp.bmp”
Image that doesn’t match:
1. “.jpg”, “.gif”,”.png”,”.bmp” – image file name is required2. ” .jpg”, ” .gif”,” .png”,” .bmp” – White space is not allow in first character
3. “a.txt”, “a.exe”,”a.”,”a.mp3″ – Only image file extension is allow
3. “jpg”, “gif”,”png”,”bmp” – image file extension is required
Unit Test – ImageValidator
| package com.mkyong.regex;  import org.testng.Assert; import org.testng.annotations.*;  /** * Image validator Testing * @author mkyong * */public class ImageValidatorTest {        private ImageValidator imageValidator;       @BeforeClass        public void initData(){            imageValidator = new ImageValidator();        }       @DataProvider      public Object[][] ValidImageProvider() {         return new Object[][]{           {new String[] {               "a.jpg", "a.gif","a.png", "a.bmp",               "..jpg", "..gif","..png", "..bmp",               "a.JPG", "a.GIF","a.PNG", "a.BMP",               "a.JpG", "a.GiF","a.PnG", "a.BmP",               "jpg.jpg", "gif.gif","png.png", "bmp.bmp"             }              }         };      }       @DataProvider      public Object[][] InvalidImageProvider() {        return new Object[][]{          {new String[] {               ".jpg", ".gif",".png",".bmp",               " .jpg", " .gif"," .png"," .bmp",                   "a.txt", "a.exe","a.","a.mp3",               "jpg", "gif","png","bmp"             }             }         };      }       @Test(dataProvider = "ValidImageProvider")       public void ValidImageTest(String[] Image) {          for(String temp : Image){               boolean valid = imageValidator.validate(temp);               System.out.println("Image is valid : " + temp + " , " + valid);               Assert.assertEquals(true, valid);         }       }       @Test(dataProvider = "InvalidImageProvider",                  dependsOnMethods="ValidImageTest")      public void InValidImageTest(String[] Image) {          for(String temp : Image){               boolean valid = imageValidator.validate(temp);               System.out.println("Image is valid : " + temp + " , " + valid);               Assert.assertEquals(false, valid);         }      }     } | 
Unit Test – Result
| Image is valid : a.jpg , trueImage is valid : a.gif , trueImage is valid : a.png , trueImage is valid : a.bmp , trueImage is valid : ..jpg , trueImage is valid : ..gif , trueImage is valid : ..png , trueImage is valid : ..bmp , trueImage is valid : a.JPG , trueImage is valid : a.GIF , trueImage is valid : a.PNG , trueImage is valid : a.BMP , trueImage is valid : a.JpG , trueImage is valid : a.GiF , trueImage is valid : a.PnG , trueImage is valid : a.BmP , trueImage is valid : jpg.jpg , trueImage is valid : gif.gif , trueImage is valid : png.png , trueImage is valid : bmp.bmp , trueImage is valid : .jpg , falseImage is valid : .gif , falseImage is valid : .png , falseImage is valid : .bmp , falseImage is valid :  .jpg , falseImage is valid :  .gif , falseImage is valid :  .png , falseImage is valid :  .bmp , falseImage is valid : a.txt , falseImage is valid : a.exe , falseImage is valid : a. , falseImage is valid : a.mp3 , falseImage is valid : jpg , falseImage is valid : gif , falseImage is valid : png , falseImage is valid : bmp , falsePASSED: ValidImageTest([Ljava.lang.String;@1d4c61c)PASSED: InValidImageTest([Ljava.lang.String;@116471f) ===============================================    com.mkyong.regex.ImageValidatorTest    Tests run: 2, Failures: 0, Skips: 0===============================================  ===============================================mkyongTotal tests run: 2, Failures: 0, Skips: 0=============================================== | 
Want to learn more about regular expression? Highly
recommend this best and classic book – “Mastering Regular Expression”
 
 
Hello! Experience the benefits of outsourcing with our reliable image object annotations services BPO . Our company, known for its approach to recruiting and retaining top talent, is ready to provide hight-quality data for your AI education process . From recruiting those specialists to support you AI systems, we have the resources to streamline your operations.
ReplyDelete