SlideShare a Scribd company logo
Regex1.1.pptx
Regex1.1.pptx
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

More Related Content

PPTX
Regular Expressions in Java
PPTX
Regular expressions
PPT
Regex Experession with Regex functions o
PPT
16 Java Regex
PDF
What are Regular Expressions in Java | Java Regex Tutorial Edureka
PDF
Java Regular Expression PART I
PDF
Java Regular Expression PART I
PDF
Regular Expressions Cheat Sheet
Regular Expressions in Java
Regular expressions
Regex Experession with Regex functions o
16 Java Regex
What are Regular Expressions in Java | Java Regex Tutorial Edureka
Java Regular Expression PART I
Java Regular Expression PART I
Regular Expressions Cheat Sheet

Similar to Regex1.1.pptx (20)

PPTX
Regex lecture
PPT
Regular Expression
PPT
Expresiones regulares, sintaxis y programación en JAVA
PPTX
Java: Regular Expression
PPT
RegEx : Expressions and Parsing Examples
PPTX
Regular expressions
ODP
CiNPA Security SIG - Regex Presentation
PPTX
Regular Expressions(Theory of programming languages))
PDF
Python Programming - XI. String Manipulation and Regular Expressions
PPTX
Mikhail Khristophorov "Introduction to Regular Expressions"
PDF
Python (regular expression)
PDF
Lecture 10.pdf
PPT
RegEx Parsing
PPTX
Regular Expression Crash Course
PDF
Python - Lecture 7
PPTX
22CS307-ADAVANCE JAVA PROGRAMMING UNIT 5
PDF
Intro To Regex In Java
PPTX
Regular Expression
ODP
DerbyCon 7.0 Legacy: Regular Expressions (Regex) Overview
PPTX
NUS_NLP__Foundations_-_Section_2_-_Words.pptx
Regex lecture
Regular Expression
Expresiones regulares, sintaxis y programación en JAVA
Java: Regular Expression
RegEx : Expressions and Parsing Examples
Regular expressions
CiNPA Security SIG - Regex Presentation
Regular Expressions(Theory of programming languages))
Python Programming - XI. String Manipulation and Regular Expressions
Mikhail Khristophorov "Introduction to Regular Expressions"
Python (regular expression)
Lecture 10.pdf
RegEx Parsing
Regular Expression Crash Course
Python - Lecture 7
22CS307-ADAVANCE JAVA PROGRAMMING UNIT 5
Intro To Regex In Java
Regular Expression
DerbyCon 7.0 Legacy: Regular Expressions (Regex) Overview
NUS_NLP__Foundations_-_Section_2_-_Words.pptx
Ad

More from VigneshK635628 (9)

PPTX
Logical_Connectives_2022 (1).pptx for final year
PPTX
Critical Reasoning.pptx for students of final year
PPTX
Impression_Management.pptx
PPTX
Averages_and_Weighted_Averages_ 2022.pptx
PPTX
Verbal Reasoning (1).pptx
PPTX
Sentence correction(Parallelism based questions) .pptx
PPTX
Geomentry 2022.pptx
PPTX
Lessons_on_Excellence_Ethics_and_Integrity_2022.pptx
PPTX
Calendars_2022.pptx
Logical_Connectives_2022 (1).pptx for final year
Critical Reasoning.pptx for students of final year
Impression_Management.pptx
Averages_and_Weighted_Averages_ 2022.pptx
Verbal Reasoning (1).pptx
Sentence correction(Parallelism based questions) .pptx
Geomentry 2022.pptx
Lessons_on_Excellence_Ethics_and_Integrity_2022.pptx
Calendars_2022.pptx
Ad

Recently uploaded (20)

DOCX
RUHS II MBBS Microbiology Paper-II with Answer Key | 6th August 2025 (New Sch...
PPTX
Imaging of parasitic D. Case Discussions.pptx
PPTX
Chapter-1-The-Human-Body-Orientation-Edited-55-slides.pptx
PPTX
Pathophysiology And Clinical Features Of Peripheral Nervous System .pptx
PPTX
Fundamentals of human energy transfer .pptx
PPT
STD NOTES INTRODUCTION TO COMMUNITY HEALT STRATEGY.ppt
PPT
Management of Acute Kidney Injury at LAUTECH
PDF
Handout_ NURS 220 Topic 10-Abnormal Pregnancy.pdf
PPTX
POLYCYSTIC OVARIAN SYNDROME.pptx by Dr( med) Charles Amoateng
PPTX
Uterus anatomy embryology, and clinical aspects
PPT
Copy-Histopathology Practical by CMDA ESUTH CHAPTER(0) - Copy.ppt
PPTX
NEET PG 2025 Pharmacology Recall | Real Exam Questions from 3rd August with D...
PPTX
LUNG ABSCESS - respiratory medicine - ppt
PDF
Human Health And Disease hggyutgghg .pdf
PPT
MENTAL HEALTH - NOTES.ppt for nursing students
PPTX
Neuropathic pain.ppt treatment managment
PPTX
neonatal infection(7392992y282939y5.pptx
PPTX
ca esophagus molecula biology detailaed molecular biology of tumors of esophagus
PPT
Obstructive sleep apnea in orthodontics treatment
PPT
OPIOID ANALGESICS AND THEIR IMPLICATIONS
RUHS II MBBS Microbiology Paper-II with Answer Key | 6th August 2025 (New Sch...
Imaging of parasitic D. Case Discussions.pptx
Chapter-1-The-Human-Body-Orientation-Edited-55-slides.pptx
Pathophysiology And Clinical Features Of Peripheral Nervous System .pptx
Fundamentals of human energy transfer .pptx
STD NOTES INTRODUCTION TO COMMUNITY HEALT STRATEGY.ppt
Management of Acute Kidney Injury at LAUTECH
Handout_ NURS 220 Topic 10-Abnormal Pregnancy.pdf
POLYCYSTIC OVARIAN SYNDROME.pptx by Dr( med) Charles Amoateng
Uterus anatomy embryology, and clinical aspects
Copy-Histopathology Practical by CMDA ESUTH CHAPTER(0) - Copy.ppt
NEET PG 2025 Pharmacology Recall | Real Exam Questions from 3rd August with D...
LUNG ABSCESS - respiratory medicine - ppt
Human Health And Disease hggyutgghg .pdf
MENTAL HEALTH - NOTES.ppt for nursing students
Neuropathic pain.ppt treatment managment
neonatal infection(7392992y282939y5.pptx
ca esophagus molecula biology detailaed molecular biology of tumors of esophagus
Obstructive sleep apnea in orthodontics treatment
OPIOID ANALGESICS AND THEIR IMPLICATIONS

Regex1.1.pptx

  • 4. ● 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
  • 5. 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.
  • 6. 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
  • 7. ● 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
  • 8. 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.
  • 9. 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
  • 10. 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
  • 11. 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
  • 12. ● 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
  • 13. 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()); } }
  • 14. 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); } }
  • 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 +91 7815 095 095 +91 9019 921 340 https://learn.codemithra.com