SlideShare a Scribd company logo
Importance and Use Cases of Advanced Stream Features
Creating Custom Streams
Stream Generators(Stream.generate,Stream.iterate)
InfiniteStream-Using Spliterators
Advanced Stream Operations - FlatMapping - Chaining Stream Operations
Stream Peeking (peek) - Advanced Filtering Techniques (seminar)
Introduction to Regular Expressions:
Character Classes - Quantifiers - Pattern Matching - Groups and Capturing
Regex in Java - java.util.regex Package Pattern Class - Matcher Class –
StringManipulation with Regex - Splitting Strings –
Replacing Text (replaceAll, replaceFirst)
Replacing with Backreferences.
Unit 5
22CS307-ADAVANCE JAVA PROGRAMMING UNIT 5
22CS307-ADAVANCE JAVA PROGRAMMING UNIT 5
The regex pattern [^a-z] finds all the occurrences of a character that is not in
a lowercase letter.
The regex pattern [^a-z] finds all the occurrences of a character that is not in a lowercase letter.
22CS307-ADAVANCE JAVA PROGRAMMING UNIT 5
Metacharacters in Regex
22CS307-ADAVANCE JAVA PROGRAMMING UNIT 5
22CS307-ADAVANCE JAVA PROGRAMMING UNIT 5
Quantifiers
• In regular expressions, quantifiers are metacharacters that specify how many times the previous character or group
should be matched.
• Quantifiers allow you to specify the number of occurrences of a character or group, making it easier to match
patterns of varying lengths.
• Quantifiers can be used to specify the number or length that part of a pattern should match or repeat.
22CS307-ADAVANCE JAVA PROGRAMMING UNIT 5
Regular Expressions in Java are provided under java.util.regex package.
This consists of 3 classes and 1 interface.
The java.util.regex package primarily consists of the following three classes as depicted below in
tabular format as follows:
22CS307-ADAVANCE JAVA PROGRAMMING UNIT 5
22CS307-ADAVANCE JAVA PROGRAMMING UNIT 5
Pattern Matching : Pattern Class:
Java Pattern Matching allows you to write more concise and readable code when working with complex
data structures. It simplifies extracting data from data structures and performing operations on them.
The pattern() method of the Pattern class in Java is used to get the regular expression which is
compiled to create this pattern. We use a regular expression to create the pattern and this method used
to get the same source expression.
22CS307-ADAVANCE JAVA PROGRAMMING UNIT 5
Groups and Capturing Regex in Java:
Regular Expression in Java Capturing groups is used to treat multiple characters as a single unit. You
can create a group using ().
Capturing groups are an extremely useful feature of regular expression matching that allow us to
query the Matcher to find out what the part of the string was that matched against a particular part of
the regular expression.
The portion of input String that matches the capturing group is saved into memory and can be
recalled using Backreference.
Backreference in the regular expression with a backslash () and then the number of the group to be
recalled
» An important feature of Regular Expressions is the ability to group
sections of a pattern, and provide alternate matches.
» The following two meta-characters are core parts of flexible Regular
Expressions
> | Alternation ('or' statement)
> () Grouping
» Consider if we know exactly subjects we are studying, and we want to find only those
subjects but nothing else. Following is the pattern:
» "I study (Java|English|Programming|Math|Islamic|HTML)"
Group can also be applied to pre-define a list of items that users are required to select from, such
as:
String pattern = (music|sport|book|movie)
With the above-defined group, users must select one of the items: music, sport, book, or movie.
The main purpose of using groups and subgroups is that we can separate the input string into
sections and extract those sections if they are matched with the defined pattern.
In java regular expressions, every pattern can have groups. And groups are indexed like in array’s
elements. The first group index starts at 0 just like array indexes.
There are two types of groups: explicit and implicit groups:
- Characters placed in brackets (()) are called explicit groups. Those that we have used in the
previous example.
- Characters NOT placed in brackets (()) are called implicit groups.
22CS307-ADAVANCE JAVA PROGRAMMING UNIT 5
Extract the individual date month and year:
Suppose now I want to extract:
•The day part
•The month part
•The year part
StringManipulation with Regex - Splitting Strings:
To split a string in Java using regular expressions. We will be using the split(String regex) method of the String
class to split this string around matches of the given regular expression.
This method works in the same way as invoking the method i.e. split(String regex, int limit) with the given
expression and a limit argument of zero. Therefore, trailing empty strings are not included in the resulting array.
Steps to split string in Java using regular expression
Following are the steps to split strings in Java using regular expression −
• Import the classes from the java.lang package.
• Create a string that contains text with various delimiters.
• Define the regular expression pattern to identify the delimiters (spaces, commas, periods).
• Use the split() method to break the string into tokens based on the defined delimiters.
• Display the number of tokens and each token.
22CS307-ADAVANCE JAVA PROGRAMMING UNIT 5
Explanation:
Replacing Text (replaceAll, replaceFirst) using regex
replaceAll:
Replace first:
Replacing with Backreferences:
• A backreference is a way to match the same text as previously matched by a capturing group.
• Capturing groups count from 1, so the first capturing group's result can be referenced with 1,
• the second with 2, and so on. 0 is a character escape for the NUL character.

More Related Content

PPT
16 Java Regex
PPT
Expresiones regulares, sintaxis y programación en JAVA
PDF
Java Regular Expression PART I
PDF
Java Regular Expression PART I
PDF
Regular Expressions Cheat Sheet
PDF
What are Regular Expressions in Java | Java Regex Tutorial Edureka
PPTX
Regex1.1.pptx
PPTX
Regular expressions
16 Java Regex
Expresiones regulares, sintaxis y programación en JAVA
Java Regular Expression PART I
Java Regular Expression PART I
Regular Expressions Cheat Sheet
What are Regular Expressions in Java | Java Regex Tutorial Edureka
Regex1.1.pptx
Regular expressions

Similar to 22CS307-ADAVANCE JAVA PROGRAMMING UNIT 5 (20)

PPT
Regex Experession with Regex functions o
PPTX
Mikhail Khristophorov "Introduction to Regular Expressions"
PPTX
Ch08 - Manipulating Data in Strings and Arrays
PDF
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
PPTX
Java: Regular Expression
PDF
Lecture 10.pdf
PDF
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
PDF
Regular expressions
PPTX
Regular Expressions
PPTX
Regular expressions
PPT
Regular expressions
PPT
Chapter Two(1)
PPT
Regular Expressions
PPTX
Regular Expressions in Java
PPTX
Regular expressions
PDF
Python (regular expression)
PPTX
Regex lecture
PDF
Regular expressions
PDF
Regular expressions
PDF
Practical JavaScript Programming - Session 6/8
Regex Experession with Regex functions o
Mikhail Khristophorov "Introduction to Regular Expressions"
Ch08 - Manipulating Data in Strings and Arrays
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
Java: Regular Expression
Lecture 10.pdf
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
Regular expressions
Regular Expressions
Regular expressions
Regular expressions
Chapter Two(1)
Regular Expressions
Regular Expressions in Java
Regular expressions
Python (regular expression)
Regex lecture
Regular expressions
Regular expressions
Practical JavaScript Programming - Session 6/8
Ad

More from logesswarisrinivasan (13)

PPT
Propostional logic and model checking.ppt
PPT
Propostional logic and model checking.ppt
PPT
Constraint Satisfaction in Artifical intelligennce.ppt
PPT
22CS307-ADAVANCE JAVA PROGRAMMING UNIT 4
PPTX
22CS305-UNIT-1.pptx ADVANCE JAVA PROGRAMMING
PPT
20CS305 Advance Java Programming Unit 1.ppt
PPTX
unit 4 for trees data structure notes it is
PPT
PPT
PPTX
CS3013 -MACHINE LEARNING.pptx
PDF
21GE301-UHV-Unit4.pdf
PPTX
PPT
Unit 2 [autosaved]
Propostional logic and model checking.ppt
Propostional logic and model checking.ppt
Constraint Satisfaction in Artifical intelligennce.ppt
22CS307-ADAVANCE JAVA PROGRAMMING UNIT 4
22CS305-UNIT-1.pptx ADVANCE JAVA PROGRAMMING
20CS305 Advance Java Programming Unit 1.ppt
unit 4 for trees data structure notes it is
CS3013 -MACHINE LEARNING.pptx
21GE301-UHV-Unit4.pdf
Unit 2 [autosaved]
Ad

Recently uploaded (20)

DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Lecture Notes Electrical Wiring System Components
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Geodesy 1.pptx...............................................
DOCX
573137875-Attendance-Management-System-original
PPTX
Welding lecture in detail for understanding
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Digital Logic Computer Design lecture notes
PPTX
additive manufacturing of ss316l using mig welding
PDF
PPT on Performance Review to get promotions
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
CH1 Production IntroductoryConcepts.pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Lecture Notes Electrical Wiring System Components
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
CYBER-CRIMES AND SECURITY A guide to understanding
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Foundation to blockchain - A guide to Blockchain Tech
Geodesy 1.pptx...............................................
573137875-Attendance-Management-System-original
Welding lecture in detail for understanding
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Digital Logic Computer Design lecture notes
additive manufacturing of ss316l using mig welding
PPT on Performance Review to get promotions

22CS307-ADAVANCE JAVA PROGRAMMING UNIT 5

  • 1. Importance and Use Cases of Advanced Stream Features Creating Custom Streams Stream Generators(Stream.generate,Stream.iterate) InfiniteStream-Using Spliterators Advanced Stream Operations - FlatMapping - Chaining Stream Operations Stream Peeking (peek) - Advanced Filtering Techniques (seminar) Introduction to Regular Expressions: Character Classes - Quantifiers - Pattern Matching - Groups and Capturing Regex in Java - java.util.regex Package Pattern Class - Matcher Class – StringManipulation with Regex - Splitting Strings – Replacing Text (replaceAll, replaceFirst) Replacing with Backreferences. Unit 5
  • 4. The regex pattern [^a-z] finds all the occurrences of a character that is not in a lowercase letter. The regex pattern [^a-z] finds all the occurrences of a character that is not in a lowercase letter.
  • 9. Quantifiers • In regular expressions, quantifiers are metacharacters that specify how many times the previous character or group should be matched. • Quantifiers allow you to specify the number of occurrences of a character or group, making it easier to match patterns of varying lengths. • Quantifiers can be used to specify the number or length that part of a pattern should match or repeat.
  • 11. Regular Expressions in Java are provided under java.util.regex package. This consists of 3 classes and 1 interface. The java.util.regex package primarily consists of the following three classes as depicted below in tabular format as follows:
  • 14. Pattern Matching : Pattern Class: Java Pattern Matching allows you to write more concise and readable code when working with complex data structures. It simplifies extracting data from data structures and performing operations on them. The pattern() method of the Pattern class in Java is used to get the regular expression which is compiled to create this pattern. We use a regular expression to create the pattern and this method used to get the same source expression.
  • 16. Groups and Capturing Regex in Java: Regular Expression in Java Capturing groups is used to treat multiple characters as a single unit. You can create a group using (). Capturing groups are an extremely useful feature of regular expression matching that allow us to query the Matcher to find out what the part of the string was that matched against a particular part of the regular expression. The portion of input String that matches the capturing group is saved into memory and can be recalled using Backreference. Backreference in the regular expression with a backslash () and then the number of the group to be recalled
  • 17. » An important feature of Regular Expressions is the ability to group sections of a pattern, and provide alternate matches. » The following two meta-characters are core parts of flexible Regular Expressions > | Alternation ('or' statement) > () Grouping » Consider if we know exactly subjects we are studying, and we want to find only those subjects but nothing else. Following is the pattern: » "I study (Java|English|Programming|Math|Islamic|HTML)"
  • 18. Group can also be applied to pre-define a list of items that users are required to select from, such as: String pattern = (music|sport|book|movie) With the above-defined group, users must select one of the items: music, sport, book, or movie. The main purpose of using groups and subgroups is that we can separate the input string into sections and extract those sections if they are matched with the defined pattern. In java regular expressions, every pattern can have groups. And groups are indexed like in array’s elements. The first group index starts at 0 just like array indexes. There are two types of groups: explicit and implicit groups: - Characters placed in brackets (()) are called explicit groups. Those that we have used in the previous example. - Characters NOT placed in brackets (()) are called implicit groups.
  • 20. Extract the individual date month and year: Suppose now I want to extract: •The day part •The month part •The year part
  • 21. StringManipulation with Regex - Splitting Strings: To split a string in Java using regular expressions. We will be using the split(String regex) method of the String class to split this string around matches of the given regular expression. This method works in the same way as invoking the method i.e. split(String regex, int limit) with the given expression and a limit argument of zero. Therefore, trailing empty strings are not included in the resulting array. Steps to split string in Java using regular expression Following are the steps to split strings in Java using regular expression − • Import the classes from the java.lang package. • Create a string that contains text with various delimiters. • Define the regular expression pattern to identify the delimiters (spaces, commas, periods). • Use the split() method to break the string into tokens based on the defined delimiters. • Display the number of tokens and each token.
  • 24. Replacing Text (replaceAll, replaceFirst) using regex replaceAll:
  • 26. Replacing with Backreferences: • A backreference is a way to match the same text as previously matched by a capturing group. • Capturing groups count from 1, so the first capturing group's result can be referenced with 1, • the second with 2, and so on. 0 is a character escape for the NUL character.