1 / 10

CS 211 Regular Expressions

CS 211 Regular Expressions. Today’s Lecture. Review Chapter 4 Go over exercises. Processing Input. If we know how to read in a line of input, what else might we want to do with it? Analyze it in some way, based on some pattern Extract certain values out of it, based on some pattern

Download Presentation

CS 211 Regular Expressions

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. CS 211Regular Expressions

  2. Today’s Lecture • Review Chapter 4 • Go over exercises

  3. Processing Input • If we know how to read in a line of input, what else might we want to do with it? • Analyze it in some way, based on some pattern • Extract certain values out of it, based on some pattern • We can create regular expressions to identify patterns, and then use them to extract the relevant info out of the pattern. • A regular expression represents a pattern • Can be used to "match"a particular string→ With Scanner’s findInLine() method • Java represents a regular expression with a String literal Regular Expressions: appendix H in the text.

  4. Special Symbols: Repetition

  5. Special Symbols: "character classes" Many more character classes can be found at:http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html

  6. Special Symbols: Pre-defined groups * note: there is a space char in this. Other whitespace chars also, but their unicode representations were omitted here.

  7. Special Symbols: everything else the backslash is used to escape any special character, so that we can match the character itself. a* matches zero or more a's a\* matches an a followed by a star\b "matches" the gap between characters, instead of a particular character. \bhe\b would match within "if he is"→ wouldn't match within "if she is" or "anthem". † here, ★ could be [,],*,+,?,{,},and so on. It's a placeholder for the special symbols, and ★ would not show up in a regular expression itself.

  8. Representing Regular Expressions in Java • We use a String literal to represent a regular expression in Java. • This means that " must be escaped: \" • This also means the \ must also be escaped! \\" (represents ") • Suggested conversion: write the regExp on paper, carefully represent each character correctly inside the String, one at a time:

  9. Let’s go over the exercises

  10. Questions?

More Related