1 / 19

JAVA RegEx

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 - $ ^ . [] ( ) + ?

kirima
Download Presentation

JAVA RegEx

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JAVA RegEx Manish Shrivastava

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

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

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

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

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

  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!

More Related