DOC PREVIEW
Penn CIT 594 - Regular Expressions in Java

This preview shows page 1-2-3-24-25-26 out of 26 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Regular Expressions in JavaRegular ExpressionsPerl and JavaA first exampleDoing it in Perl and RubyDoing it in Java, IDoing it in Java, IIFinding what was matchedA complete exampleAdditional methodsSome simple patternsSequences and alternativesSome predefined character classesBoundary matchersGreedy quantifiersTypes of quantifiersQuantifier examplesCapturing groupsCapturing groups in JavaExample use of capturing groupsDouble backslashesEscaping metacharactersSpacesAdditions to the String classThinking in regular expressionsThe EndJan 13, 2019Regular Expressions in Java2Regular ExpressionsA regular expression is a kind of pattern that can be applied to text (Strings, in Java)A regular expression either matches the text (or part of the text), or it fails to matchIf a regular expression matches a part of the text, then you can easily find out which partIf a regular expression is complex, then you can easily find out which parts of the regular expression match which parts of the textWith this information, you can readily extract parts of the text, or do substitutions in the textRegular expressions are an extremely useful tool for manipulating textRegular expressions are heavily used in the automatic generation of Web pages3Perl and JavaThe Perl programming language is heavily used in server-side programming, becauseMuch server-side programming is text manipulationRegular expressions are built into the syntax of PerlBeginning with Java 1.4, Java has a regular expression package, java.util.regexJava’s regular expressions are almost identical to those of PerlThis new capability greatly enhances Java 1.4’s text handlingRegular expressions in Java 1.4 are just a normal package, with no new syntax to support themJava’s regular expressions are just as powerful as Perl’s, butRegular expressions are easier and more convenient in Perl4A first exampleThe regular expression "[a-z]+" will match a sequence of one or more lowercase letters [a-z] means any character from a through z, inclusive + means “one or more”Suppose we apply this pattern to the String "Now is the time"There are three ways we can apply this pattern:To the entire string: it fails to match because the string contains characters other than lowercase lettersTo the beginning of the string: it fails to match because the string does not begin with a lowercase letterTo search the string: it will succeed and match owIf applied repeatedly, it will find is, then the, then time, then fail5Doing it in Perl and RubyIn both Perl and Ruby, a regular expression is written between forward slashes, for example, /[a-z]+/Regular expressions are values, and can be used as suchFor example, line.split(/\s+/)We can search for matches to a regular expression with the =~ operatorFor example, name = "Dave"; name =~ /[a-z]/; will find ave6Doing it in Java, IFirst, you must compile the pattern import java.util.regex.*; Pattern p = Pattern.compile("[a-z]+");Next, you must create a matcher for a specific piece of text by sending a message to your pattern Matcher m = p.matcher("Now is the time");Points to notice:Pattern and Matcher are both in java.util.regexNeither Pattern nor Matcher has a public constructor; you create these by using methods in the Pattern classThe matcher contains information about both the pattern to use and the text to which it will be applied7Doing it in Java, IINow that we have a matcher m,m.matches() returns true if the pattern matches the entire text string, and false otherwisem.lookingAt() returns true if the pattern matches at the beginning of the text string, and false otherwisem.find() returns true if the pattern matches any part of the text string, and false otherwiseIf called again, m.find() will start searching from where the last match was foundm.find() will return true for as many matches as there are in the string; after that, it will return false When m.find() returns false, matcher m will be reset to the beginning of the text string (and may be used again)8Finding what was matchedAfter a successful match, m.start() will return the index of the first character matchedAfter a successful match, m.end() will return the index of the last character matched, plus oneIf no match was attempted, or if the match was unsuccessful, m.start() and m.end() will throw an IllegalStateExceptionThis is a RuntimeException, so you don’t have to catch itIt may seem strange that m.end() returns the index of the last character matched plus one, but this is just what most String methods requireFor example, "Now is the time".substring(m.start(), m.end()) will return exactly the matched substring9A complete exampleimport java.util.regex.*; public class RegexTest { public static void main(String args[]) { String pattern = "[a-z]+"; String text = "Now is the time"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(text); while (m.find()) { System.out.print(text.substring(m.start(), m.end()) + "*"); } }}Output: ow*is*the*time*10Additional methodsIf m is a matcher, thenm.replaceFirst(replac ement) returns a new String where the first substring matched by the pattern has been replaced by repla c e mentm.replaceAll(re placement) returns a new String where every substring matched by the pattern has been replaced by repla c e mentm.find(startIndex) looks for the next pattern match, starting at the specified indexm.reset() resets this matcherm.reset(newText) resets this matcher and gives it new text to examine (which may be a String, StringBuffer, or CharBuffer)11Some simple patternsabc exactly this sequence of three letters[abc] any one of the letters a, b, or c[^abc] any character except one of the letters a, b, or c(immediately within an open bracket, ^ means “not,” but anywhere else it just means the character ^)[a-z] any one character from a through z, inclusive[a-zA-Z0-9] any one letter or digit12Sequences and alternativesIf one pattern is followed by another, the two patterns must match consecutivelyFor example, [A-Za-z]+[0-9] will match one or more letters immediately followed by one digitThe vertical bar, |, is used to separate alternativesFor example, the pattern abc|xyz will match either abc or xyz13Some predefined character classes. any one character except a line terminator\d a digit:


View Full Document

Penn CIT 594 - Regular Expressions in Java

Documents in this Course
Trees

Trees

17 pages

Searching

Searching

24 pages

Pruning

Pruning

11 pages

Arrays

Arrays

17 pages

Stacks

Stacks

30 pages

Recursion

Recursion

25 pages

Hashing

Hashing

24 pages

Recursion

Recursion

24 pages

Graphs

Graphs

25 pages

Storage

Storage

37 pages

Trees

Trees

21 pages

Arrays

Arrays

24 pages

Hashing

Hashing

24 pages

Recursion

Recursion

25 pages

Graphs

Graphs

23 pages

Graphs

Graphs

25 pages

Stacks

Stacks

25 pages

Recursion

Recursion

25 pages

Quicksort

Quicksort

21 pages

Quicksort

Quicksort

21 pages

Graphs

Graphs

25 pages

Recursion

Recursion

25 pages

Searching

Searching

24 pages

Counting

Counting

20 pages

HTML

HTML

18 pages

Recursion

Recursion

24 pages

Pruning

Pruning

11 pages

Graphs

Graphs

25 pages

Load more
Download Regular Expressions in Java
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Regular Expressions in Java and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Regular Expressions in Java 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?