java lect 18
Post on 27-Feb-2018
216 Views
Preview:
TRANSCRIPT
-
7/25/2019 Java Lect 18
1/19
01/16/16
JAVA RegEx
Manish Shrivastava
-
7/25/2019 Java Lect 18
2/19
Recap
A regular expression-- a pattern thatdescribes or atches a set o! strings
E"g" ca#trn$
Matched text% chun& o! text 'hichatches the regular expression" E"g" ca[trn] matches car, can, cat
-
7/25/2019 Java Lect 18
3/19
Recap
MetacharactersMetacharacters -$^ . \ [] \( \) + ?
SetsSets [aeiou] andand#bcd!gh(&lnp)rstv'x*+$
Negation using (^) e.g.Negation using (^) e.g. [^a^!]
"e#eated match- using"e#eated match- using andand+ %.g. a or[a-&]
-
7/25/2019 Java Lect 18
4/19
-
7/25/2019 Java Lect 18
5/19
-
7/25/2019 Java Lect 18
6/19
iport (ava"util"regex"
2h* this ac&age3
2hat it contains3
4o' to use this ac&age3
2hat this ac&age can do3
-
7/25/2019 Java Lect 18
7/19
2h* this ac&age
Ever*thing in Java should be anob(ect o! soe class"
Regular expressions are aniportant part o! an* applications
5ote Regex ac&age 'as onl* introduceda!ter J781"9
Regular expressions (RegEx) tend to
be easier to write than they are to read
-
7/25/2019 Java Lect 18
8/19
2hat it contains
,he ac&age denes : classes attern% ;A regular expression, specifed
as a string, must frst be compiled intoan instance o this class
Matcher- An engine that per!ors atch
operations on a character se)uenceb*interpreting a attern
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/CharSequence.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/lang/CharSequence.html -
7/25/2019 Java Lect 18
9/19
4o' to use (ava"util"regex
Step 1
-
7/25/2019 Java Lect 18
10/19
2hat can it do
Match RegularExpressions
?7uh@
-
7/25/2019 Java Lect 18
11/19
Demo
ut ho'3
copile regular expression into an instance o!attern using attern"copile(regex) Eg" attern pat B attern"copile?;#ab$cdC@=
Dse the resulting pattern to create a Matcherob(ect using pattern-naeF"atcher(input) Eg" Matcher atcher B pat"atcher?CaccccdC@=
Gind i! the pattern HpatI atched
Eg" boolean isMatch B atcher"atches?@=All this can also be done b* boolean isMatch B attern"atches?;#ab$cdC>
;accccdC@=
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html -
7/25/2019 Java Lect 18
12/19
7eep 2aters
oth attern and Matcher classesprovide various !unctions
Most o! the coon operationsare provided as !unctions
-
7/25/2019 Java Lect 18
13/19
attern Gunctions
static atterncompile?Stringregex@.opiles the given regular
expression into a pattern
Matchermatcher?.harSe)uence
input@.reates a atcher that 'ill atchthe given input against this pattern"
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/lang/CharSequence.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/lang/CharSequence.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html -
7/25/2019 Java Lect 18
14/19
Stringpattern?@Returns the regular expression !ro'hich this pattern 'as copiled"
String[] split?.harSe)uenceinput@Splits the given input se)uence
around atches o! this pattern"
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/lang/CharSequence.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/lang/CharSequence.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html -
7/25/2019 Java Lect 18
15/19
static boolean matches?Stringregex> .harSe)uenceinput@
.opiles the given regularexpression and attepts to atchthe given input against it"
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/lang/CharSequence.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/lang/CharSequence.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html -
7/25/2019 Java Lect 18
16/19
Matcher !unctions
boolean fnd?@Attepts to nd the next subse)uence o! theinput se)uence that atches the pattern"
boolean lookingAt?@Attepts to atch the input se)uence>starting at the beginning> against thepattern"
booleanmatches?@Attepts to atch the entire input se)uenceagainst the pattern"
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.html -
7/25/2019 Java Lect 18
17/19
atternpattern?@Returns the pattern that is interpreted b*this atcher"
StringreplaceAll?String replaceent@Replaces ever* subse)uence o! the inputthat atches the pattern 'ith the givenstring"
String replaceFirst?String replaceent@Replaces the rst subse)uence o! the inputthat atches the pattern 'ith the givenstring"
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html -
7/25/2019 Java Lect 18
18/19
-
7/25/2019 Java Lect 18
19/19
top related