Showing posts with label Regular Expressions. Show all posts
Showing posts with label Regular Expressions. Show all posts

Java: Regular Expressions by example

Regular Expression is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. "find and replace"-like operations using a specialized syntax held in a pattern. In Java "java.util.regex" package primarily uses classes like

  • Pattern - representation of a regular expression
  • Matcher - engine that interprets the pattern and performs match operations against an input string
  • PatternSyntaxException - unchecked exception that indicates a syntax error in a regular expression pattern

Regular Expressions
Now lets see simple example Pattern and Matcher for strings to match like username, password, email, ipaddress, 12/24 time formats. Here we are going to see only whether given string matches with the given pattern or not. Apart from that we can also use Pattern for replacing particular pattern of sub-string from the given the given string etc.,

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

public class MyRegExTest {

 public static void main(String[] args) {
  
  Pattern pattern;
  Matcher matcher;
  
  //USERNAME
  String patUsername = "^[a-z0-9_-]{4,15}$";
  
  pattern = Pattern.compile(patUsername);
  matcher = pattern.matcher("javadiscover");
  
  System.out.println("USERNMAE 1 : "+ matcher.matches());
  System.out.println("USERNAME 2 : "+ pattern.matcher("qw").matches());
 
  
  //PASSWORD
  String patPassword = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{8,20})";
  pattern = Pattern.compile(patPassword);
  
  System.out.println("PASSWORD 1 : "+ pattern.matcher("123asd").matches());
  System.out.println("PASSWORD 2 : "+ pattern.matcher("fhGe#123").matches());
  
  //IPADDRESS
  String patIp = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
  pattern = Pattern.compile(patIp);
  
  System.out.println("IP 1 : "+ pattern.matcher("123.234.19.0").matches());
  System.out.println("IP 2 : "+ pattern.matcher("257.2.99.4").matches());
  
  
  //EMAIL
  String patEmail = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
  pattern = Pattern.compile(patEmail);
  
  System.out.println("EMAIL 1 : "+ pattern.matcher("javadiscover@gmail.com").matches());
  System.out.println("EMAIL 2 : "+ pattern.matcher("spam@spam").matches());
  
  //TIME - 12-HOURS
  String patTime12 = "(1[012]|[1-9]):[0-5][0-9](\\s)?(?i)(am|pm)";
  pattern = Pattern.compile(patTime12);
  
  System.out.println("TIME 1 : "+ pattern.matcher("12:34 AM").matches());
  System.out.println("TIME 2 : "+ pattern.matcher("13:67 PM").matches());
  
  //TIME - 24-HOURS
  String patTime24 = "([01]?[0-9]|2[0-3]):[0-5][0-9]";
  pattern = Pattern.compile(patTime24);
  
  System.out.println("TIME 1 : "+ pattern.matcher("15:17").matches());
  System.out.println("TIME 2 : "+ pattern.matcher("13:67").matches());
 }
}


OUTPUT:

USERNMAE 1 : true
USERNAME 2 : false

PASSWORD 1 : false
PASSWORD 2 : true

IP 1 : true
IP 2 : false

EMAIL 1 : true
EMAIL 2 : false

TIME 1 : true
TIME 2 : false

TIME 1 : true
TIME 2 : false