Professional Documents
Culture Documents
04 Regularexpression
04 Regularexpression
Regular Expression
Session objectives
– Introduction
– Pattern match in Java
– Simple patterns
– Character classes
– Boundary matchers
– Types of quantifiers
– Capturing Groups
– Others problem
2
Regular Expressions
4
Pattern match in Java
6
Patterns Flags
7
Methods of Matcher class
8
Matchcer methods
• It may seem strange that m.end() returns the index of the last
character matched plus one, but this is just what most String
methods require
o For example, "Now is the time".substring(m.start(), m.end()) will
return exactly the matched substring
10
A complete example
11
Additional methods
13
Some simple patterns
Pattern Description
[ab^c] a, b, ^ or c.
[a-z&&[def]] d, e, or f (intersection)
a through z, except for b and c: [ad-z]
[a-z&&[^bc]]
(subtraction)
a through z, and not m through p: [a-lq-z]
[a-z&&[^m-p]]
(subtraction) 15
Sequences and alternatives
16
Some predefined Character Classes
17
Boundary matchers
\b a word boundary
\Z the end of the input except for the final terminator, if any
Output
19
19/28
Greedy quantifiers
21
Quantifiers examples
Output
23
Capturing Groups - 1
matched digits
o In addition, \0 holds everything matched by the entire pattern
25
Example of Capturing Groups
Output
26
Example of Numbering Capture Group
Output
27
27/28
Example use of capturing groups
30
Spaces
32
Thinking in regular expressions
35
35/27