In java we can achieve validations on data through pattern matching. For that classes are avaliable in
java.util.regex package.
java.util.regex.Pattern is the class which deals with the pattern matching.
lets have an example where we have to validate a mobile no provided by the user.
package com.pack.pattern;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MobilePattern {
public static void main(String[] args) {
String userdata;
System.out.println("Enter the mobile number");
Scanner sc=new Scanner(System.in);
userdata=sc.nextLine();
String mypattern="[7-9][0-9]{9}"; /*1*/
Pattern p=Pattern.compile(mypattern); /*2*/
Matcher m=p.matcher(userdata); /*3*/
if(m.find() && m.group().equals(userdata)) /*4*/
{
System.out.println("valid moblile number");
}
else
System.out.println("Invalid number");
}
}
Pattern is a class in java which is used the validate the data . we can create the object of pattern by using a method name compile (Static method) which takes input as a string (The desired pattern )
Matcher is another class in regex package which deals with matching the data with provided pattern.
In Matcher class we have boolean method named find() and group()
How to make a pattern ?
we make a pattern in string format so it should be enclosed between " "(double courts).
e.g
To enter 10 numbers
String pattern="[0-9]{10}"; // this will check numbers only not
To enter String or characters
String pattern="[a-zA-Z]{5}"; //here we can provide only characters between in small or capital letters
* indicates many times at least one.
+ indicates one or more times
? indicates may be or not
a(variable without sign) represent required one time.its mandatory
Program to match the email id from the given data by the user.
package com.pack.pattern;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailPattern {
public static void main(String[] args) {
String userdata;
System.out.println("Enter the Email id");
Scanner sc=new Scanner(System.in);
userdata=sc.nextLine();
String mypattern="[a-zA-Z][_A-Za-z0-9]*@[A-Za-z]+([.][A-Za-z]+)+";
Pattern p=Pattern.compile(mypattern);
Matcher m=p.matcher(userdata);
if(m.find() && m.group().equals(userdata))
{
System.out.println("valid email number");
}
else
System.out.println("Invalid email");
}
}
java.util.regex package.
java.util.regex.Pattern is the class which deals with the pattern matching.
lets have an example where we have to validate a mobile no provided by the user.
package com.pack.pattern;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MobilePattern {
public static void main(String[] args) {
String userdata;
System.out.println("Enter the mobile number");
Scanner sc=new Scanner(System.in);
userdata=sc.nextLine();
String mypattern="[7-9][0-9]{9}"; /*1*/
Pattern p=Pattern.compile(mypattern); /*2*/
Matcher m=p.matcher(userdata); /*3*/
if(m.find() && m.group().equals(userdata)) /*4*/
{
System.out.println("valid moblile number");
}
else
System.out.println("Invalid number");
}
}
Pattern is a class in java which is used the validate the data . we can create the object of pattern by using a method name compile (Static method) which takes input as a string (The desired pattern )
Matcher is another class in regex package which deals with matching the data with provided pattern.
In Matcher class we have boolean method named find() and group()
How to make a pattern ?
we make a pattern in string format so it should be enclosed between " "(double courts).
e.g
To enter 10 numbers
String pattern="[0-9]{10}"; // this will check numbers only not
To enter String or characters
String pattern="[a-zA-Z]{5}"; //here we can provide only characters between in small or capital letters
* indicates many times at least one.
+ indicates one or more times
? indicates may be or not
a(variable without sign) represent required one time.its mandatory
Program to match the email id from the given data by the user.
package com.pack.pattern;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailPattern {
public static void main(String[] args) {
String userdata;
System.out.println("Enter the Email id");
Scanner sc=new Scanner(System.in);
userdata=sc.nextLine();
String mypattern="[a-zA-Z][_A-Za-z0-9]*@[A-Za-z]+([.][A-Za-z]+)+";
Pattern p=Pattern.compile(mypattern);
Matcher m=p.matcher(userdata);
if(m.find() && m.group().equals(userdata))
{
System.out.println("valid email number");
}
else
System.out.println("Invalid email");
}
}