Thursday 21 December 2017

How to write a regex to validate the image name | Regex in java


Click here to watch on Youtube :
https://www.youtube.com/watch?v=sJiEmnnbFSc&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
How to write a regex to validate the image name | Regex in java
ImageValidator.java

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * How to validate image file extension with regular expression
 *
 */

public class ImageValidator
{

    private Pattern pattern;
    private Matcher matcher;

    private static final String IMAGE_NAME_REGEX = "([^\\s]+(\\.(?i)(jpg|png|gif|bmp))$)";

    public ImageValidator()
    {
        pattern = Pattern.compile(IMAGE_NAME_REGEX);
    }

    /**
     * Validate image with regular expression
     *
     * @param imageName imageName for validation
     * @return true valid imageName, false invalid imageName
     */

    public boolean validate(final String imageName)
    {
        matcher = pattern.matcher(imageName);
        return matcher.matches();
    }
}

RegexDemo.java

import java.util.Arrays;
import java.util.List;

/**
 *
 * How to validate image file extension with regular expression
 *
 */

public class RegexDemo
{

    public static void main(String[] args)
    {
        List<String> validImageNameList = Arrays.asList("cat.jpg", "cat.gif",
                "cat.png", "cat.bmp", "dog.JPG", "dog.GIF", "dog.PNG",
                "dog.BMP","rose.JpG", "rose.gIF", "rose.PNg",
                "rose.BMp");

        ImageValidator imageValidator = new ImageValidator();
       
        for (String imageName : validImageNameList)
        {
            System.out.println(imageName + " is Valid? = " + imageValidator.validate(imageName));
        }

        System.out.println("--------------------------------------");

        List<String> inValidImageNameList = Arrays.asList(".jpg", " .gif","dog.txt","jpg");
       
        for (String imageName : inValidImageNameList)
        {
            System.out.println(imageName + " is Valid? = " + imageValidator.validate(imageName));
        }

    }

}

Output

cat.jpg is Valid? = true
cat.gif is Valid? = true
cat.png is Valid? = true
cat.bmp is Valid? = true
dog.JPG is Valid? = true
dog.GIF is Valid? = true
dog.PNG is Valid? = true
dog.BMP is Valid? = true
rose.JpG is Valid? = true
rose.gIF is Valid? = true
rose.PNg is Valid? = true
rose.BMp is Valid? = true
--------------------------------------
.jpg is Valid? = false
 .gif is Valid? = false
dog.txt is Valid? = false
jpg is Valid? = false

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_image_name.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/RegexDemo_image_name

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/576579fab6c376d5ecd7cb5b7402b7fd1a7c4ae3/BasicJava/RegexDemo_image_name/?at=master

See also:

  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment