regex

Regular Expression and Pattern Matching Algorithms are discussed here

Pattern Matching Example problem (pull down)

import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.Scanner;
public class Hello 
{
    static String PASSWORD="((?=.*\\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[_!@#$%&]).{8,20})"; 
    
    // =  -->is to check that the condition should be matched.
    // !  --> is to check that condition sould not have in that.
    //(?=.*\\d)  --> for integer or it can be given as (?=.[0-9])
    //(?!.*[A-Z])  --> to say that it should not contain any uppercase.
    
    public static void main(String[] args) 
    {
        Scanner sc=new Scanner(System.in);
        String password=sc.nextLine();
        Pattern pattern=Pattern.compile(PASSWORD);
        Matcher matcher=pattern.matcher(password);
        if(password.length()<8)
        {
            System.out.print("Length should not be less than 8");
            return;
        }
        if(matcher.matches())    //or  while(matcher.find()) --> can also be used to validate.
        {
            System.out.print("VALID");
        }
        else
        {
            System.out.print("INVALID");
        }
        
  }
}

input 1:
Gowtham@258

output 1:
VALID

input 2:
gowtham258

output 2:
INVALID

input 3:
abc@123

output 3:
Length should not be less than 8

Functions in REGEX classes

Matcher class :

It implements the MatchResult interface. It is a regex engine which is used to perform match operations on a character sequence.


boolean matches()

test whether the regular expression matches the pattern.

boolean find()

finds the next expression that matches the pattern.

boolean find(int start)

finds the next expression that matches the pattern from the given start number.

String group()

returns the matched subsequence.

int start()

returns the starting index of the matched subsequence.

int end()

returns the ending index of the matched subsequence.

int groupCount()

returns the total number of the matched subsequence.

Pattern class :

It is the compiled version of a regular expression. It is used to define a pattern for the regex engine.


static Pattern compile(String regex)

compiles the given regex and returns the instance of the Pattern.

Matcher matcher(CharSequence input)

creates a matcher that matches the given input with the pattern.

static boolean matches(String regex, CharSequence input)

It works as the combination of compile and matcher methods. It compiles the regular expression and matches the given input with the pattern.

String[] split(CharSequence input)

splits the given input string around matches of given pattern.

String pattern()

returns the regex pattern.

Regex Character classes :


[abc]

a, b, or c (simple class)

[^abc]

Any character except a, b, or c (negation)

[a-zA-Z]

a through z or A through Z, inclusive (range)

[a-d[m-p]]

a through d, or m through p: [a-dm-p] (union)

[a-z&&[def]]

d, e, or f (intersection)

[a-z&&[^bc]]

a through z, except for b and c: [ad-z] (subtraction)

[a-z&&[^m-p]]

a through z, and not m through p: [a-lq-z](subtraction)

Regex Quantifiers :


X?

X occurs once or not at all

X+

X occurs once or more times

X*

X occurs zero or more times

X{n}

X occurs n times only

X{n,}

X occurs n or more times

X{y,z}

X occurs at least y times but less than z times

Regex Meta characters :


Any character (may or may not match terminator)

\d

Any digits, short of [0-9]

\D

Any non-digit, short for [^0-9]

\s

Any whitespace character, short for [\t\n\x0B\f\r]

\S

Any non-whitespace character, short for [^\s]

\w

Any word character, short for [a-zA-Z_0-9]

\W

Any non-word character, short for [^\w]

\b

A word boundary

\B

A non word boundary

import java.util.regex.*;  

public class Main
{  
public static void main(String args[])
{  

//1st way  
Pattern p = Pattern.compile(".s");
Matcher m = p.matcher("as");  
boolean b = m.matches();  
  
//2nd way  
boolean b2=Pattern.compile(".s").matcher("as").matches();  
  
//3rd way  
boolean b3 = Pattern.matches(".s", "as");  
  
System.out.println(b+" "+b2+" "+b3);  
}
}  

output:
true true true
import java.util.regex.Pattern;  
import java.util.Scanner;  
import java.util.regex.Matcher;    
public class Main{    
    public static void main(String[] args){    
        Scanner sc=new Scanner(System.in);  
        while (true) {    
            System.out.println("Enter regex pattern:");  
            Pattern pattern = Pattern.compile(sc.nextLine());    
            System.out.println("Enter text:");  
            Matcher matcher = pattern.matcher(sc.nextLine());    
            boolean found = false;    
            while (matcher.find())
             {    
                System.out.println("I found the text "+matcher.group()+" starting at index "+    
                 matcher.start()+" and ending at index "+matcher.end());    
                found = true;    
            }    
            if(!found){    
                System.out.println("No match found.");    
            }    
        }    
    }    
}  

input:
Enter regex pattern: java
Enter text: this is java, do you know java

output:
I found the text java starting at index 8 and ending at index 12
I found the text java starting at index 26 and ending at index 30
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
  
class Main 
{ 
    public static void main(String args[]) 
    { 
        String text = "hello1welcome2to3codejamm4"; 
  
        // Specifies the string pattern which is to be searched 
        String delimiter =  "\\d"; 
        Pattern pattern = Pattern.compile(delimiter, 
                                        Pattern.CASE_INSENSITIVE); 
  
        String[] result = pattern.split(text); 
  
        for (String temp: result) 
            System.out.println(temp); 
    } 
} 

output:
hello
welcome
to
codejamm
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
  
class Reg 
{ 
    public static void main(String[] args) 
    { 
        String txt = "welcomeToCodeJamm"; 
  
        // Demonstrating ^ 
        String regex1 = "^welcome"; 
        Pattern pattern1 = Pattern.compile(regex1, Pattern.CASE_INSENSITIVE); 
        Matcher matcher1 = pattern1.matcher(txt); 
        while (matcher1.find()) 
        { 
            System.out.println("Start index: " + matcher1.start()); 
            System.out.println("End index: " + matcher1.end()); 
        } 
  
        // Demonstrating $ 
        String regex2 = "CodeJamm$"; 
        Pattern pattern2 = Pattern.compile(regex2, Pattern.CASE_INSENSITIVE); 
        Matcher matcher2 = pattern2.matcher(txt); 
        while (matcher2.find()) 
        { 
            System.out.println("\nStart index: " + matcher2.start()); 
            System.out.println("End index: " + matcher2.end()); 
        } 
    } 
} 

output:
Start index: 0
End index: 6

Start index: 9
End index: 16
Program to print only the digits from a string

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

public class RegexExamples 
{
    public static void main(String[]args) 
{
        Pattern p = Pattern.compile("\\d+");
        Matcher m = p.matcher("string1234more567string890");
        while(m.find()) 
        {
            System.out.println(m.group());
        }
    }
}

output:
1234
567
890
Extract the Nth digit from a string

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

public class RegexExamples 
{
    private static final Pattern p = Pattern.compile("[^\\d]*[\\d]+[^\\d]+([\\d]+)");
    public static void main(String[] args)
 { 
        Matcher m = p.matcher("string1234more567string890");
        if (m.find()) 
        {
            System.out.println(m.group(1)); 
        }
    }
}

output:
567
Extract key-Value pairs from a regular expression

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

public class RegexExamples {
    public static void main(String[] args) {
        String s = "bookname=cooking&bookid=123456&bookprice=123.45";
        Pattern p = Pattern.compile("(?<=bookid=)\\d+");
        Matcher m = p.matcher(s);
        if (m.find()) {
            System.out.println(m.group());
        }
    }
}

output:
12345
sum of 2 and 3 digit integers in a string using REGEX:

import java.util.regex.*;
import java.util.*;
public class Main
{
  public static void main (String[]args)
  {
    Scanner sc = new Scanner (System.in);
      List < String > al = new ArrayList <> ();
    String str = sc.nextLine ();
    Pattern p = Pattern.compile ("\\d+");
    Matcher m = p.matcher (str);
    while (m.find ())
      {
  al.add (m.group ());
      }
    long sum = 0;
    int flag = 0;
    for (int i = 0; i < al.size (); i++)
      {
 if (al.get (i).length () == 2 || al.get (i).length () == 3)
   {
     sum += Long.parseLong (al.get (i));
     flag = 1;
   }
      }
    if (flag == 0)
      System.out.print ("-1");
    else
      System.out.print (sum);
  }
}

input 1:
i have tim1 167 290 785:dsjd 46 fjh 34 

output 1:
1322