Presentation is loading. Please wait.

Presentation is loading. Please wait.

JAVA RegEx Manish Shrivastava 11/11/2018.

Similar presentations


Presentation on theme: "JAVA RegEx Manish Shrivastava 11/11/2018."— Presentation transcript:

1 JAVA RegEx Manish Shrivastava 11/11/2018

2 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

3 Recap Metacharacters -$ ^ . \ [] \( \) + ?
Sets [aeiou] and [bcdfghjklmnpqrstvwxyz] Negation using (^) – e.g. [^ab^8] Repeated match- using * and + E.g. a* or [a-z]*

4 Thank you !

5 Java and RegEx Package java.util.regex Description java.util.regex
Classes for matching character sequences against patterns specified by regular expressions. java.util.regex java.util.regex.Pattern java.util.regex.Matcher

6 import java.util.regex.*
Why this Package? What it contains? How to use this Package? What this Package can do?

7 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”

8 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

9 How to use java.util.regex
Step 1 : Import import java.util.regex.*; Alternatively, import java.util.regex.Pattern; import java.util.regex.Matcher;

10 Match Regular Expressions
What can it do Match Regular Expressions (Duh!!!)

11 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 Demo now

12 Deep Waters Both Pattern and Matcher classes provide various functions
Most of the common operations are provided as functions

13 Pattern Functions static Pattern compile(String regex) Compiles the given regular expression into a pattern  Matcher matcher(CharSequence input) Creates a matcher that will match the given input against this pattern.

14  String pattern() Returns the regular expression from which this pattern was compiled.
 String[] split(CharSequence input) Splits the given input sequence around matches of this pattern.

15 static boolean matches(String regex, CharSequence input) Compiles the given regular expression and attempts to match the given input against it.

16 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.

17 Pattern pattern() Returns the pattern that is interpreted by this matcher.
String replaceAll(String replacement) Replaces every subsequence of the input that matches the pattern with the given string. String replaceFirst(String replacement) Replaces the first subsequence of the input that matches the pattern with the given string.

18 Int start() Returns the start index of the previous match.
 int end() Returns the index of the last character matched, plus one.

19 Thank You!


Download ppt "JAVA RegEx Manish Shrivastava 11/11/2018."

Similar presentations


Ads by Google