Regular Expressions

Matching the 1st and Last Character

4 points

 

Introduction

The character '^' in a regular expression is used to locate words that begin with a specific character. For example, ^m would identify words that begin with 'm'. Similarly, the '$' is used to locate words that end with a specific character. An example: m$ would select words that end with 'm'. In the video linked to below, this idea is discussed at 11:00.

A period '.' in a regular expression can represent any character. Following a character with '*' matches with 0 or more appearances of any character. So .* is quite general, and will match any number of any characters.

In the input file (regexInput4.txt) are many words, most that begin and end with 'l' (the letter) and some that begin and end with '1' (the number). You want to locate the '1' words, and you want to add each of them on to a cumulative String. When you print the result at the end, you should get a meaningful result. You'll want to use a regular expression that locates words that both begin and end with '1'.

 

Example

Download and run regexExample4.java. Modify 'regex' to try to match words that begin with a given character or end with a given character.

 

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

 

To Get Started

Download regexInput4.txt, the input file. Also download regex4.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