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@258output 1:VALIDinput 2:gowtham258output 2:INVALIDinput 3:abc@123output 3:Length should not be less than 8It 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.
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.
[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)
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
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 trueimport 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: javaEnter text: this is java, do you know javaoutput:I found the text java starting at index 8 and ending at index 12I found the text java starting at index 26 and ending at index 30import 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:hellowelcometocodejammimport 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: 0End index: 6Start index: 9End index: 16Program to print only the digits from a stringimport 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:1234567890Extract the Nth digit from a stringimport 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:567Extract key-Value pairs from a regular expressionimport 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:12345sum 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