Guest User

Regex

a guest
Mar 8th, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.71 KB | None | 0 0
  1. import java.util.regex.*;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner input = new Scanner(System.in);
  8.         String fullName = "";
  9.         String gender = "";
  10.         int age = 0; //done
  11.         String dob = "";
  12.         String cnic = "";
  13.         String email = "";
  14.         String address = "";
  15.         String cellPhone = "";
  16.         String landLine = "";
  17.         String username = "";
  18.         String password = "";
  19.  
  20.  
  21.         System.out.println("FullName (SPECIAL CHARACTERS AND NUMBERS ARE NOT ALLOWED, ONLY SALUTATIONS ARE!)");
  22.         fullName = input.nextLine();
  23.         while(!regexChecker("^[A-Za-z\\s]{1,}[\\.]{0,1}[A-Za-z\\s]{0,}$", fullName)){
  24.             System.out.println("Invalid Format, please renter your FullName with the proper format");
  25.             fullName = input.nextLine();
  26.         }
  27.  
  28.         System.out.println("Gender (MALE / FEMALE / M / F)");
  29.         gender = input.nextLine();
  30.         while(!regexChecker("^M|MALE|F|FEMALE$", gender)){
  31.             System.out.println("Invalid Gender, please renter your Gender with the proper Format!");
  32.             gender = input.nextLine();
  33.         }
  34.  
  35.         System.out.println("Age (YOU MUST BE OLDER THAN 18 YEARS)");
  36.         age = input.nextInt();
  37.         if(age < 18){
  38.             System.out.println("Below 18 are not allowed to enter, terminating program!");
  39.             System.exit(0);
  40.         }
  41.  
  42.         System.out.println("Date of Birth (DD/MM/YYY)");
  43.         dob = input.nextLine();
  44.         while(!regexChecker("^(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)$", dob)){
  45.             System.out.println("Invalid Format/Date, please renter your DOB with the proper format");
  46.             dob = input.nextLine();
  47.         }
  48.  
  49.         System.out.println("CNIC number");
  50.         cnic = input.nextLine();
  51.         while(!regexChecker("^([0-9]{6})( |-)?([0-9]{6})( |-)?([0-9]{1})$", cnic)){
  52.             System.out.println("Invalid CNIC, please renter your CNIC with the proper format");
  53.             cnic = input.nextLine();
  54.         }
  55.  
  56.         System.out.println("Email Address");
  57.         email = input.nextLine();
  58.         while(!regexChecker("^[A-Za-z0-9._%-]+@[A-Za-z0-9]+\\.[A-Za-z]{2,4}$", email)){
  59.             System.out.println("Invalid Format, please renter your Email with the proper format");
  60.             email = input.nextLine();
  61.         }
  62.  
  63.         System.out.println("Address");
  64.         address = input.nextLine();
  65.         while(!regexChecker("^(.){0,50}$", address)){
  66.             System.out.println("Your address should not be longer than 50 characters");
  67.             address = input.nextLine();
  68.         }
  69.  
  70.         System.out.println("Cellphone number");
  71.         cellPhone = input.nextLine();
  72.         while(!regexChecker("^(((00|\\+)?(923))[0-9]{9})|((03)[0-9]{9})$", cellPhone)){
  73.             System.out.println("Invalid number, please renter your number with the proper Format!");
  74.             cellPhone = input.nextLine();
  75.         }
  76.  
  77.         System.out.println("Landline number (STARTS WITH 021 OR 3)");
  78.         landLine = input.nextLine();
  79.         while(!regexChecker("^((021)( |-)?)?(3)[0-9]{7}$", landLine)){
  80.             System.out.println("Invalid number, please renter your number with the proper Format!");
  81.             landLine = input.nextLine();
  82.         }
  83.  
  84.         System.out.println("Username");
  85.         username = input.nextLine();
  86.         while(!regexChecker("^(.){0,6}$", username)) {
  87.             System.out.println("Your username should not be longer than 6 characters");
  88.             username = input.nextLine();
  89.         }
  90.  
  91.         System.out.println("Password (ATLEAST 1 LETTER IN CAPITAL, ATLEAST 1 NUMBER, ATLEAST LETTER IN SMALL");
  92.         password = input.nextLine();
  93.         while (!regexChecker("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d]{8,}$", password)) {
  94.             System.out.println("Invalid");
  95.             password = input.nextLine();
  96.         }
  97.         System.out.println("Full Name" + fullName);
  98.         System.out.println("Gender" +  gender);
  99.         System.out.println("Age" + age);
  100.         System.out.println("Date of Birth" + dob);
  101.         System.out.println("CNIC" + cnic);
  102.         System.out.println("Email" + email);
  103.         System.out.println("Address" + address);
  104.         System.out.println("Cell Phone" + cellPhone);
  105.         System.out.println("Landline" + landLine);
  106.         System.out.println("Username" + username);
  107.         System.out.println("Password" + password);
  108.     }
  109.  
  110.     public static boolean regexChecker(String theRegex, String str2Check) {
  111.         Pattern checkRegex = Pattern.compile(theRegex);
  112.         Matcher regexMatcher = checkRegex.matcher(str2Check);
  113.         return regexMatcher.find();
  114.  
  115.     }
  116. }
Add Comment
Please, Sign In to add comment