go to  ForumEasy.com   
JavaPro
Home » Archive » Message


[Email To Friend][View in Live Context][prev topic « prev post | next post » next topic]
  Regular expression examples
 
Subject: Regular expression examples
Author: Alex_Raj
In response to: Lookahead assertion examples
Posted on: 08/13/2015 11:26:47 PM

Example: Ipv4 address

String REGEX_IPv4 = "(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])" + 
                    "\\." + 
                    "(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)" +
                    "\\." +
                    "(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)" +
                    "\\." +
                    "(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[0-9])";




Example: Ipv6 address
String REGEX_IPv6 = "(?i)" + "[[a-f][0-9]:/.+*%\\[\\]]+";




Example: Email address
String REGEX_EMAIL = "[a-zA-Z0-9_+.%\\-]{1,256}" +
                     "\\@" +
                     "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
                     "(" + 
                     "\\.[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
                     ")+";




Example: WEB URL
String WEB_URL = "(?i)" + 
                 "\\b(https?|ftp|file)://" + 
                 "[a-zA-Z0-9+&@#/%?=~_|!:,.;\\-]*[a-zA-Z0-9+&@#/%=~_|\\-]";



Example: Password strength validity

The password strength criteria requires:
8+ characters length 
1+ Special Character
1+ letters in Upper Case
2+ numerals (0-9)
3+ letters in Lower Case


String REGEX_PWD = 
  "^" +                          // starting      
  "" +                           // empty string   
  "(?=.*[\\W])" +                // followed by at least 1 special char   
  "(?=.*[A-Z])" +                // followed by at least 1 upper-case char
  "(?=.*[0-9].*[0-9])" +         // followed by at least 2 digits
  "(?=.*[a-z].*[a-z].*[a-z])" +  // followed by at least 3 lower-case chars
  ".{8,}" +                      // followed by any (.) chars with total length of at least 8
  "$";                           // ending



 

> On 08/13/2015 07:16:49 PM Alex_Raj wrote:

Example: Find empty string '' which is immediately followed by lower-case character
    String regex = "(?=[a-z])"; 
    String input = "123a567b9";
    System.out.println("Regex: " + regex);
    System.out.println("Input: " + input);
	
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(input);
		
    boolean found = false;
    while (matcher.find()) {
        System.out.println("Found '" + matcher.group() + 
                           "' from " + matcher.start() + 
                           " to " + matcher.end());
        found = true;
    }

    if(!found)
        System.out.println("No match found!");



The ouput:

    Regex: (?=[a-z])
    Input: 123a567b9
    Found '' from 3 to 3
    Found '' from 7 to 7




Example: Find string which 1) starts with '' and 2) is immediately followed by lower-case character and 3) has total length of 2 any characters
    String regex = "(?=[a-z]).{2}"; 
    String input = "123a567b9";



The ouput:

    Regex: (?=[a-z]).{2}
    Input: 123a567b9
    Found 'a5' from 3 to 5
    Found 'b9' from 7 to 9




Example: Find string which 1) starts with '' and 2) is immediately followed by lower-case character and 3) has total length of 3 any characters
    String regex = "(?=[a-z]).{3}"; 
    String input = "123a567b9";



The ouput:

    Regex: (?=[a-z]).{3}
    Input: 123a567b9
    Found 'a56' from 3 to 6






References:

 


 
Powered by ForumEasy © 2002-2022, All Rights Reserved. | Privacy Policy | Terms of Use
 
Get your own forum today. It's easy and free.