260 likes | 538 Views
JAVA RegEx. Manish Shrivastava. Recap. A regular expression-- a pattern that describes or matches a set of strings E.g. ca[trn] Matched text– chunk of text which matches the regular expression. E.g. ca[trn] matches car, can, cat. Recap. Metacharacters - $ ^ . [] ( ) + ?
E N D
JAVA RegEx Manish Shrivastava
Recap • A regular expression-- a pattern that describes or matches a set of strings • E.g. ca[trn] • Matched text– chunk of text which matches the regular expression. • E.g. ca[trn] matches car, can, cat
Recap • Metacharacters -$^ . \ [] \( \) + ? • Sets[aeiou] and[bcdfghjklmnpqrstvwxyz] • Negation using (^) – e.g. [^ab^8] • Repeated match- using * and + • E.g. a* or [a-z]*
Java and RegEx • Package java.util.regex Description • Classes for matching character sequences against patterns specified by regular expressions. • java.util.regex • java.util.regex.Pattern • java.util.regex.Matcher
import java.util.regex.* • Why this Package? • What it contains? • How to use this Package? • What this Package can do?
Why this Package • Everything in Java should be an object of some class. • Regular expressions are an important part of many applications • Note : Regex Package was only introduced after JDK1.4 • “Regular expressions (RegEx) tend to be easier to write than they are to read”
What it contains • The Package defines 2 classes • Pattern – “A regular expression, specified as a string, must first be compiled into an instance of this class” • Matcher - An engine that performs match operations on a character sequence by interpreting a Pattern
How to use java.util.regex • Step 1 : Import • import java.util.regex.*; • Alternatively, • import java.util.regex.Pattern; • import java.util.regex.Matcher;
What can it do Match Regular Expressions (Duh!!!)
But how? • compile regular expression into an instance of Pattern using Pattern.compile(regex) • Eg. Pattern pat = Pattern.compile(“[ab]c*d"); • Use the resulting pattern to create a Matcher object using <pattern-name>.matcher(input) • Eg. Matcher matcher = pat.matcher("accccd"); • Find if the pattern ‘pat’ matched • Eg. boolean isMatch = matcher.matches(); • All this can also be done by • boolean isMatch = Pattern.matches(“[ab]c*d", “accccd"); Demo
Deep Waters • Both Pattern and Matcher classes provide various functions • Most of the common operations are provided as functions
Pattern Functions • static Patterncompile(String regex)Compiles the given regular expression into a pattern • Matchermatcher(CharSequence input) Creates a matcher that will match the given input against this pattern.
Stringpattern()Returns the regular expression from which this pattern was compiled. • String[] split(CharSequence input)Splits the given input sequence around matches of this pattern.
static boolean matches(String regex, CharSequence input)Compiles the given regular expression and attempts to match the given input against it.
Matcher functions • boolean find()Attempts to find the next subsequence of the input sequence that matches the pattern. • boolean lookingAt()Attempts to match the input sequence, starting at the beginning, against the pattern. • boolean matches()Attempts to match the entire input sequence against the pattern.
Patternpattern()Returns the pattern that is interpreted by this matcher. • StringreplaceAll(String replacement)Replaces every subsequence of the input that matches the pattern with the given string. • StringreplaceFirst(String replacement)Replaces the first subsequence of the input that matches the pattern with the given string.
Int start()Returns the start index of the previous match. • int end()Returns the index of the last character matched, plus one.