Question
For each of the following specifications, write a UNIX regular expression that matches all lines (strings) according to the specification. All lines containing a C++/Java
For each of the following specifications, write a UNIX regular expression that matches all lines (strings) according to the specification.
-
All lines containing a C++/Java comment of the form
//...
Recall that the comment need not start at the beginning of a line.
-
All lines starting with a lower-case letter and ending with a lower-case letter.
-
All lines that do not end with a digit unless this digit is 2.
Note: Write your expressions in one text file called prob1.txt, one expression per line. Enclose each expression between the characters ^ $. Your expressions will be tested with the given Java program testREXfile.java.
testREXfile.java :
/** * This program uses a loop that reads, in each iteration, a regular expression and a text file name, and prints all lines of the text file matching the regular expression. */ import java.util.*; import java.io.*; import java.util.regex.Pattern; import java.util.regex.Matcher; public class testREXfile { public static void main(String[] args) { Scanner kbd = new Scanner(System.in); System.out.println(" ==============================================="); System.out.println("WELCOME TO THE REGULAR EXPRESSION FILE MATCHER"); System.out.println("given a regular expression R and a text file F"); System.out.println(" the program prints all lines of F matching R"); while (true) { System.out.println("----------------------------"); System.out.println("Enter a UNIX regular expression: "); String str = kbd.nextLine(); Pattern rex = Pattern.compile(str); System.out.println("Enter the name of a text file: "); String txt = kbd.nextLine(); try { Scanner in = new Scanner(new FileReader(txt)); String line; int cnt = 0; while (in.hasNextLine()) { line = in.nextLine(); cnt = cnt + 1; Matcher m = rex.matcher(line); if (m.find()) { System.out.println(cnt + ": " + line); } } in.close(); // Close to unlock } catch (Exception e) { System.err.println(e); System.exit(0); } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started