Regular Expressions

Locating Email Addresses

5 points

 

Introduction

What makes a valid email address? An example: jstevens@metuchenhigh.org. The format appears to be: name @ destination . com/net/org/us. If any of these elements is missing, the email address won't work.

 

In this program, you start with a file (EmailAddresses.txt) which has what appear to be many attempts to write valid email addresses. Most of the attempts are flawed, but there are several valid email addresses in the file. Your job is to use a regular expression to extract the valid email addresses.

 

In regular expressions in Java, "\\w" matches with a "word character." A word character is a-z (any lower-case letter from a to z), A-Z, 0-9 or _ (underscore). To match with one or more word characters, the regular expression would be "\\w+". The + means "one or more of those." The + is discussed in the video linked to below, starting at 3:45.

 

In regular expressions, a "." is a metacharacter that will match any one character (except a newline). If, however, you actually want to match for a period, and not treat "." as a metacharacter, you do that in Java with "\\." An example that uses this idea begins at 12:00 in the video linked below. (In the example in the video, you'll see it as \. not \\.)

 

Another useful concept in regular expressions is a "group." A group is put in parentheses, and anything inside the group can cause a match. For example (cat|dog|fish) would match either cat, dog or fish. The "|" acts as an "or." Groups are discussed in the video linked to below, starting at 8:30.

 

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 the regular expression features discussed above to write a regular expression that will find the valid email addresses in EmailAddresses.txt. When your program finds a valid email address, it should print it.

 

To Get Started

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