SlideServe
Menu
Préférences
© SlideServe · v2026

JAVA RegEx

kirima avatar
19 diapositives · 587 Views · Jul 14, 2014

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

JAVA RegEx - slide 1
01 / 19JAVA RegEx
1 / 17

Télécharger la présentation

JAVA RegEx
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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!