JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to validate file extensions using regular expression in java?


Description:

The below given example shows how to validate a file extensions using regular expression. The regular expression allows txt, doc, csv, and pdf file extensions only.


Code:
package com.java2novice.regex;

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

public class MyFileExtenValidation {

private static Pattern fileExtnPtrn = Pattern.compile("([^\\s]+(\\.(?i)(txt|doc|csv|pdf))$)");
	
	public static boolean validateFileExtn(String userName){
		
		Matcher mtch = fileExtnPtrn.matcher(userName);
		if(mtch.matches()){
			return true;
		}
		return false;
	}
	
	public static void main(String a[]){
		System.out.println("Is 'java2novice.pdf' allowed file? "
						+validateFileExtn("java2novice.pdf"));
		System.out.println("Is 'cric.doc' allowed file? "
						+validateFileExtn("cric.doc"));
		System.out.println("Is 'java.gif' allowed file? "
						+validateFileExtn("java.gif"));
		System.out.println("Is 'novice.mp3' allowed file? "
						+validateFileExtn("novice.mp3"));
		System.out.println("Is 'java_2.jpeg' allowed file? "
						+validateFileExtn("java_2.jpeg"));
	}
}

Output:
Is 'java2novice.pdf' allowed file? true
Is 'cric.doc' allowed file? true
Is 'java.gif' allowed file? false
Is 'novice.mp3' allowed file? false
Is 'java_2.jpeg' allowed file? false
<< Previous Program | Next Program >>

List Of Regular Expression Sample Programs:

  1. Simple regex pattern matching using string matches().
  2. Simple java regex using Pattern and Matcher classes.
  3. Java regex with case insensitive.
  4. How to validate IP address using regular expression?
  5. How to replace a pattern using regular expression in java?
  6. How to remove multiple spaces with a single space with in a string?
  7. How to validate user name using regular expression?
  8. How to validate email address using regular expression?
  9. How to validate password using regular expression?
  10. How to validate file extensions using regular expression in java?
  11. How to validate date format using regular expression in java?
  12. How to capture or extract a value(s) from text using regular expression in java?
  13. How to split a string using regular expression?
Knowledge Centre
Purpose of garbage collection
The garbage collection process is to identify the objects which are no longer referenced or needed by a program so that their resources can be reclaimed and reused. These identified objects will be discarded.
Famous Quotations
It is easier to fight for one’s principles than to live up to them.
-- Alfred Adler

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.