REGEX IN JAVA
● The Java Regex or Regular Expression is an API to define a pattern for
searching or manipulating strings
● Java Regex API provides 1 interface and 3 classes
in java.util.regex package
Introduction
What is a Regular Expression?
A regular expression is a sequence of characters that forms a search pattern.
When you search for data in a text, you can use this search pattern to describe
what you are searching for.
A regular expression can be a single character, or a more complicated pattern.
Regular expressions can be used to perform all types of text search and text
replace operations.
java.util.regex package
The java.util.regex package provides following classes and interfaces
for regular expressions.
● MatchResult interface
● Matcher class
● Pattern class
● PatternSyntaxException class
REGEX PACKAGE
● it is the compiled version of a regular expression
● It is used to define a pattern for the regex engine
● Pattern class doesn’t have any public constructor and we use it’s
public static method compile to create the pattern object by passing
regular expression as an argument
PATTERN CLASS
PATTERN METHODS
static Pattern compile(String regex)
Compiles the given regular expression
into a pattern
Matcher matcher(CharSequence input)
Creates a matcher that will match the
given input against this pattern
static boolean matches(String regex, CharSequence
input)
Compiles the given regular expression
and attempts to match the given input
against it
String[] split(CharSequence input)
Splits the given input sequence around
matches of this pattern.
public static Pattern compile(String regex)
Compiles the given regular expression into a pattern.
Parameters:
regex - The expression to be compiled
Throws:
PatternSyntaxException - If the expression's syntax is invalid
COMPILE
public Matcher matcher(CharSequence input)
Creates a matcher that will match the given input against this pattern.
Parameters:
input - The character sequence to be matched
Returns:
A new matcher for this pattern
MATCHER
public static boolean matches(String regex,CharSequence
input)
● Compiles the given regular expression and attempts to match the
given input against it.
● An invocation of this convenience method of the form
Pattern.matches(regex, input);
● behaves in exactly the same way as the expression
Pattern.compile(regex).matcher(input).matches()
MATCHES
● if a pattern is to be used multiple times, compiling it once and reusing
it will be more efficient than invoking this method each time.
Parameters:
● regex - The expression to be compiled
● input - The character sequence to be matched
Throws:
● PatternSyntaxException - If the expression's syntax is invalid
MATCHES
EXAMPLE
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile(".xx.");
Matcher matcher = pattern.matcher("MxxY");
System.out.println("Input String matches regex -
"+matcher.matches());
}
}
EXAMPLE -2 (REGEX EXAMPLE IN JAVA)
import java.util.regex.*;
public class Main {
public static void main(String args[]){
Pattern p = Pattern.compile(".s");
Matcher m = p.matcher("as");
boolean b = m.matches();
boolean b2=Pattern.compile(".s").matcher("as").matches();
boolean b3 = Pattern.matches(".s", "as");
System.out.println(b+" "+b2+" "+b3);
}
}
The . (dot) represents a single character.
REGULAR EXPRESSION .
import java.util.regex.*;
class Main{
public static void main(String args[]){
System.out.println(Pattern.matches(".s", "as"));
System.out.println(Pattern.matches(".s", "mk"));
}
}
REGEX CHARACTER CLASSES
[abc] a, b, or c (simple class)
[^abc] Any character except a, b, or c (negation)
[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 META CHARACTERS
. Any character (may or may not
match line terminators)
d A digit: [0-9]
D A non-digit: [^0-9]
s A whitespace character: [
tnx0Bfr]
S A non-whitespace character: [^s]
REGEX QUANTIFIERS
Regex Description
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
/ethnuscodemithra /ethnus
Ethnus
Codemithra
/code_mithra
codemithra@ethnus.com +91 7815 095
095
+91 9019 921
340
https://learn.codemithra.com

Regex1.1.pptx

  • 3.
  • 4.
    ● The JavaRegex or Regular Expression is an API to define a pattern for searching or manipulating strings ● Java Regex API provides 1 interface and 3 classes in java.util.regex package Introduction
  • 5.
    What is aRegular Expression? A regular expression is a sequence of characters that forms a search pattern. When you search for data in a text, you can use this search pattern to describe what you are searching for. A regular expression can be a single character, or a more complicated pattern. Regular expressions can be used to perform all types of text search and text replace operations.
  • 6.
    java.util.regex package The java.util.regexpackage provides following classes and interfaces for regular expressions. ● MatchResult interface ● Matcher class ● Pattern class ● PatternSyntaxException class REGEX PACKAGE
  • 7.
    ● it isthe compiled version of a regular expression ● It is used to define a pattern for the regex engine ● Pattern class doesn’t have any public constructor and we use it’s public static method compile to create the pattern object by passing regular expression as an argument PATTERN CLASS
  • 8.
    PATTERN METHODS static Patterncompile(String regex) Compiles the given regular expression into a pattern Matcher matcher(CharSequence input) Creates a matcher that will match the given input against this pattern static boolean matches(String regex, CharSequence input) Compiles the given regular expression and attempts to match the given input against it String[] split(CharSequence input) Splits the given input sequence around matches of this pattern.
  • 9.
    public static Patterncompile(String regex) Compiles the given regular expression into a pattern. Parameters: regex - The expression to be compiled Throws: PatternSyntaxException - If the expression's syntax is invalid COMPILE
  • 10.
    public Matcher matcher(CharSequenceinput) Creates a matcher that will match the given input against this pattern. Parameters: input - The character sequence to be matched Returns: A new matcher for this pattern MATCHER
  • 11.
    public static booleanmatches(String regex,CharSequence input) ● Compiles the given regular expression and attempts to match the given input against it. ● An invocation of this convenience method of the form Pattern.matches(regex, input); ● behaves in exactly the same way as the expression Pattern.compile(regex).matcher(input).matches() MATCHES
  • 12.
    ● if apattern is to be used multiple times, compiling it once and reusing it will be more efficient than invoking this method each time. Parameters: ● regex - The expression to be compiled ● input - The character sequence to be matched Throws: ● PatternSyntaxException - If the expression's syntax is invalid MATCHES
  • 13.
    EXAMPLE import java.util.regex.*; public classMain { public static void main(String[] args) { Pattern pattern = Pattern.compile(".xx."); Matcher matcher = pattern.matcher("MxxY"); System.out.println("Input String matches regex - "+matcher.matches()); } }
  • 14.
    EXAMPLE -2 (REGEXEXAMPLE IN JAVA) import java.util.regex.*; public class Main { public static void main(String args[]){ Pattern p = Pattern.compile(".s"); Matcher m = p.matcher("as"); boolean b = m.matches(); boolean b2=Pattern.compile(".s").matcher("as").matches(); boolean b3 = Pattern.matches(".s", "as"); System.out.println(b+" "+b2+" "+b3); } }
  • 15.
    The . (dot)represents a single character. REGULAR EXPRESSION . import java.util.regex.*; class Main{ public static void main(String args[]){ System.out.println(Pattern.matches(".s", "as")); System.out.println(Pattern.matches(".s", "mk")); } }
  • 16.
    REGEX CHARACTER CLASSES [abc]a, b, or c (simple class) [^abc] Any character except a, b, or c (negation) [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)
  • 17.
    REGEX META CHARACTERS .Any character (may or may not match line terminators) d A digit: [0-9] D A non-digit: [^0-9] s A whitespace character: [ tnx0Bfr] S A non-whitespace character: [^s]
  • 18.
    REGEX QUANTIFIERS Regex Description 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
  • 19.
    /ethnuscodemithra /ethnus Ethnus Codemithra /code_mithra codemithra@ethnus.com +917815 095 095 +91 9019 921 340 https://learn.codemithra.com