Regular Expressions

Positive LookBehind

3 points

 

Introduction

You can use a regular expression to match characters at the start of a line or word. This is called a "positive lookbehind." In this program, you'll read in a lot of words, while looking for words that begin with 'z'. You'll want to print these words to an output file.

 

The syntax for a positive lookbehind is: (?<=INF) where INF is what you're looking for at the start of the word. The INF at the start of the word will not be included in the match; what is matched is what follows INF. Following INF will be a word that you want to include in the output, so use the regular expression syntax for "one or more word characters." This was discussed in regex exercise 6. Positive lookbehind is discussed at 12:30 in the video linked to below.

 

Regexr: Feel free to experiment with any of these regular expressions ideas at https://regexr.com, which is also linked below.

 

So your task is to use a positive lookbehind to find those words that begin with 'z'. Print these words to an output file, and a well-known quote (attributed to Ralph Waldo Emerson) will appear. (It's not altogether clear that Emerson actually ever said or wrote this quote.)

 

To Get Started

Download LookBehind.txt, the input file. Also download regex7.java, which is the program that you'll modify.

 

Resources

Regexr website
https://regexr.com

 

Video:
https://www.youtube.com/watch?v=rhzKDrUiJVk

 

Regular Expressions tutorial (Oracle)
https://docs.oracle.com/javase/tutorial/essential/regex/

 

Pattern class in Java API:
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/regex/Pattern.html

 

Matcher class in Java API:
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/regex/Matcher.html