D.Tech Academic
Deep Singh
Pages
Core Java
Web App
Spring
Hibernate
Kotlin
Android
Python
Flutter
Spring Boot
Regex Example in java
1. How to find words have uppercase
public class Example1 { public static void main(String[] args) { String msg="hello frIends how1 r U ?"; String t[]=msg.split(" "); for(int i=0;i
2. How to find words have number
public class Example2 { public static void main(String[] args) { String msg="1hello fr1ends how1 r ?"; String t[]=msg.split(" "); for(int i=0;i
2. How to find words have special char
public class Example3 { public static void main(String[] args) { String msg="1hello fr1e%ds how1 r ?"; String t[]=msg.split(" "); for(int i=0;i
4. How to split string from multiple white space
public class Example4 { public static void main(String[] args) { String msg="hello friends how r u ?"; String t[]=msg.split("\\s+"); for(int i=0;i
5. How to find vowels from given string
public class Example5 { public static void main(String[] args) { String msg="hello friends how r u ?"; String s=msg.replaceAll("[BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz]",""); System.out.println(s); } }
6. Password pattern, Email Pattern, White Space Pattern, Mobile Pattern, Digit with Letter pattern example
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Regex { String emailPattern="^[a-zA-Z0-9]{1,20}@[a-zA-Z]{1,20}.[a-zA-Z]{2,3}$"; String mobilePattern="\\d{10}"; String fullName="^[a-zA-Z\\s]+"; String passwordPattern="((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!?&()@$%#]).{6,15})"; String dwl="(.*)(\\d+)(.*)"; public boolean digitwithletter(String data){ Pattern p=Pattern.compile(dwl); Matcher m=p.matcher(data); if(m.find()){ //System.out.println(m.group()); return true; }else{ return false; } } public boolean emailValidation(String email){ Pattern pattern=Pattern.compile(emailPattern); Matcher matcher=pattern.matcher(email); if(!matcher.matches()){ return false; }else{ return true; } } public void printNamesWithOutSpaces(String names){ String data[]=names.split("\\s+"); for(String n:data){ System.out.println(n); } } public boolean mobileNumber(String mobile){ Pattern pattern=Pattern.compile(mobilePattern); Matcher matcher=pattern.matcher(mobile); if(!matcher.matches()){ return false; }else{ return true; } } public boolean fullName(String name){ Pattern pattern=Pattern.compile(fullName); Matcher matcher=pattern.matcher(name); if(!matcher.matches()){ return false; }else{ return true; } } public boolean passwordValidation(String password){ Pattern pattern=Pattern.compile(passwordPattern); Matcher matcher=pattern.matcher(password); if(!matcher.matches()){ return false; }else{ return true; } } public static void main(String[] args) { Regex r=new Regex(); //System.out.println(r.digitwithletter("deep1"));; //System.out.println(r.emailValidation("deep44@gmail.com")); //r.printNamesWithOutSpaces("my name is deep "); //System.out.println(r.mobileNumber("8224898008")); //System.out.println(r.fullName("Deep singh")); System.out.println(r.passwordValidation("eWp$d1h")); } }
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment