Professional Documents
Culture Documents
In This Session, You Will Learn To:: Objectives
In This Session, You Will Learn To:: Objectives
Objectives
Slide 1 of 27
Ver. 1.0
Programming in Java
Using the split() Method
split() method:
Used to parse a string
Breaks down the string apart when applied with character/s
The following embedded Word document shows the
utilization of the split()method.
split method
Slide 2 of 27
Ver. 1.0
Programming in Java
Parsing with StringTokenizer
StringTokenizer class:
Used to parse a string
Allows accessing tokens by iteration
The embedded Word document demonstrates the utilization
of the StringTokenizer class.
StringTokenizer
class
Slide 3 of 27
Ver. 1.0
Programming in Java
Scanner Class
Scanner class:
Used to tokenize an input stream or a string
Used to tokenize numbers and convert them into primitive type
The embedded Word document shows how to tokenize a
string or a stream using the Scanner class.
Scanner class
Slide 4 of 27
Ver. 1.0
Programming in Java
Regular Expressions
Regular expressions:
Used to match strings of text
Provide detailed vocabulary
Used to search and extract or replace strings
Java objects used in regular expression:
Pattern
Matcher
PatternSyntaxException
java.util.regex
Slide 5 of 27
Ver. 1.0
Programming in Java
Pattern and Matcher
Pattern and
Matcher classes
Slide 6 of 27
Ver. 1.0
Programming in Java
Character Class Patterns
[^abc] Would match any character that is not “a”, “b”, or “c” in that position
[a-c] A range of characters (in this case, “a”, “b”, and “c”)
Slide 7 of 27
Ver. 1.0
Programming in Java
Character Class Patterns (Contd.)
w.s Any sequence that starts with a “w” It was the best of times
followed by any character followed by “s”.
w[abc]s Any sequence that starts with a “w” It was the best of times
followed by “a”, “b”, or “c” and then “s”.
t[^eao]mes Any sequence that starts with a “t” followed It was the best of times
by any character that is not “a”, “e” or “o”
followed by “mes”.
Slide 8 of 27
Ver. 1.0
Programming in Java
Activity: CustomCharClassExample
Slide 9 of 27
Ver. 1.0
Programming in Java
Predefined Character Classes
Slide 10 of 27
Ver. 1.0
Programming in Java
Predefined Character Classes (Contd.)
Slide 11 of 27
Ver. 1.0
Programming in Java
Activity: PredefinedCharClassExample
Slide 12 of 27
Ver. 1.0
Programming in Java
Quantifiers
Slide 13 of 27
Ver. 1.0
Programming in Java
Quantifier: Examples
Slide 14 of 27
Ver. 1.0
Programming in Java
Activity: QuantifiersExample
Slide 15 of 27
Ver. 1.0
Programming in Java
Greediness
Greediness principle:
Regular expression grabs as many characters as possible
? operator:
Limits the search to the shortest possible match
Target string: Longlonglong ago, in a galaxy far far away.
The following table shows greediness and the usage of ?
operator on the preceding target string.
Pattern Description Text Matched
ago.*far A regular expression always grabs the Longlonglong ago, in a galaxy far
most characters possible. far away.
ago.*?far The “?” character essentially turns off Longlonglong ago, in a galaxy far
greediness. far away.
Slide 16 of 27
Ver. 1.0
Programming in Java
Activity: GreedinessExample
Slide 17 of 27
Ver. 1.0
Programming in Java
Boundary Matchers
Slide 18 of 27
Ver. 1.0
Programming in Java
Boundary: Examples
^it.*?times The sequence starts a line with “it” it was the best of times or it was the
followed by some characters and worst of times
ends a line with “times”, and the
greediness is off
\\sit.*times$ The sequence that starts with “it” it was the best of times or it was the
followed by some characters and worst of times
ends the line with “times”
\\bor\\b.{3} Find “or” surrounded by word it was the best of times or it was the
boundaries, plus the next three worst of times
characters
Slide 19 of 27
Ver. 1.0
Programming in Java
Matching and Groups
Pattern (\\S+?)\\.(\\S+?)@(\\S+)
Slide 20 of 27
Ver. 1.0
Programming in Java
Activity: MatchingExample
Slide 21 of 27
Ver. 1.0
Programming in Java
Using the replaceAll Method
replaceAll
method
Slide 22 of 27
Ver. 1.0
Programming in Java
Activity: ReplacingExample
Slide 23 of 27
Ver. 1.0
Programming in Java
Quiz
Slide 24 of 27
Ver. 1.0
Programming in Java
Quiz (Contd.)
Solution:
Regular expressions is a language for matching strings of text.
Slide 25 of 27
Ver. 1.0
Programming in Java
Quiz (Contd.)
Solution:
?
Slide 26 of 27
Ver. 1.0
Programming in Java
Summary
Slide 27 of 27
Ver. 1.0