Question

Regular Expression processor in Java Overview: Create a Java program that will accept a regular expression...

Regular Expression processor in Java Overview: Create a Java program that will accept a regular expression and a filename for a text file. The program will process the file, looking at every line to find matches for the regular expression and display them. Regular Expression Format The following operators are required to be accepted: + - one or more of the following character (no groups) * - zero or more of the following character (no groups) [] – no negation, no character spans – the only format is explicit, for example [0123456789] Output: The output from your program should be formatted like this: Match found on line 10, starting at position 10 and ending at position 25: aaaaaaaaaaaaaa0 If the regular expression entered was a*[01] And the input file contained at line 10: Xxdsdsasdaaaaaaaaaaaaaaa0 Restrictions Your program must implement its own regular expression functionality – you can not use the built-in regular expression mechanism, nor can you include one from a package or JAR file unless you created it and included the source code in your submission Testing Please be sure to test your code thoroughly – it will be graded using regular expressions and files that you do not have access to. Your code must work for all regular expressions and text files as described above. While you MAY NOT share code in any way, I would encourage all of you to write tests and share them with each other. You can write and test your test cases against sites like : https://regex101.com/ or using Unix/Mac grep Some Design Hints Break this down into two problems: 1) Build a list of states from the regular expression 2) Loop over the input lines, then over each character in each input line, checking the input against the states. I built a class to wrap String, adding the idea of which character is “current” and the ability to get the current character and to peek at the next character. This made my code cleaner and easier to reason about.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • import java.util.*;

    import java.io.*;

    class Regex {

    public static void main(String args[]) {

    String pattern = args[0];

    BufferedReader br = new BufferedReader(new FileReader(args[2]));

    String line;

    int lineNumber = 1;

    while((line = br.readLine()) != null){

    findMatches(line, pattern, lineNumber);

    lineNumber++;

    }

    }

    public void findMatches(String s, String p, int lineNumber) {

    int n = s.length();

    String temp;

    for(int i=0;i<n;i++){

    for(int j=i+1;j<=n;j++){

    temp = s.substring(i, j);

    if(isMatch(temp, p)){

    System.out.println("Match found on line " + lineNumber + ", starting at position " + (i+1) + " and ending at position " + j + ": " + temp);

    }

    }

    }

    }

    public boolean isMatch(String s, String p){

    boolean[][] d = new boolean[s.length() + 1][p.length() + 1];

    d[0][0] = true;

    for (int i = 0; i < p.length(); ++i) {

    char current = p.charAt(i);

    if (current == '*') {

    for (int j = 0; j < s.length(); ++j) {

    d[j + 1][i + 1] = d[j + 1][i - 1];

    }

    for (int j = 0; j < s.length(); ++j) {

    if ((p.charAt(i - 1) == '.') || (p.charAt(i - 1) == s.charAt(j))) {

    d[j + 1][i + 1] = d[j + 1][i + 1] || d[j][i - 1] || d[j][i + 1];

    }

    }

    } else if (current == '.') {

    for (int j = s.length() - 1; j >= 0; --j) {

    d[j + 1][i + 1] = d[j][i];

    }

    } else {

    for (int j = 0; j < s.length(); ++j) {

    if (s.charAt(j) == p.charAt(i)) {

    d[j + 1][i + 1] = d[j][i];

    }

    }

    }

    }

    return d[s.length()][p.length()];

    }

    }

Add a comment
Know the answer?
Add Answer to:
Regular Expression processor in Java Overview: Create a Java program that will accept a regular expression...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with java programming. Here is what I need to do: Write a Java program...

    Need help with java programming. Here is what I need to do: Write a Java program that could help test programs that use text files. Your program will copy an input file to standard output, but whenever it sees a “$integer”, will replace that variable by a corresponding value in a 2ndfile, which will be called the “variable value file”. The requirements for the assignment: 1.     The input and variable value file are both text files that will be specified in...

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

  • For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java...

    For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...

  • For this discussion, you will be working with regular expressions. Please create a regular expression that...

    For this discussion, you will be working with regular expressions. Please create a regular expression that requires passwords to begin with an uppercase letter, followed by five lowercase letters, followed by a one-digit number (0-9), and ending with one symbol (@, $, %, or &). You may use the index.htm file included with this discussion to test your regular expression. Note that the index.htm file contains HTML and JavaScript. To test your regular expression, simply assign your regular expression to...

  • Create a Java program that analyzes a program and produces the output into another file. The...

    Create a Java program that analyzes a program and produces the output into another file. The program must retrieve the path to the .java file. Once the path to the file is found the program must figure out the length of the longest line is in the program. For example, working with a program called MainFile.java. Once it figures out the longest line in the code it will put the output into MainFile.java.stats and it will be located in the...

  • Java I: Create a Java program that will accept the price of an item and the...

    Java I: Create a Java program that will accept the price of an item and the quantity being purchased of that item. After accepting the input, calculate the amount of the purchase (based on the price and quantity), calculate the sales tax on the purchase, then output the product price, quantity, subtotal, sales tax, and the total sale based on the output format shown below. Structure your file name and class name on the following pattern: The first three letters...

  • Write a java program for the following: Your program reads an infix expression represented by a...

    Write a java program for the following: Your program reads an infix expression represented by a string S from the standard input (the keyboard). Then your program converts the infix expression into a postfix expression P using the algorithm. Next, your program evaluates the postfix expression P to produce a single result R. At last, your program displays the original infix expression S, the corresponding postfix expression P and the final result R on the standard output ( the screen...

  • JAVA 6. Create two files and name them: puzzle.txt and puzzle2.txt Inside puzzle.txt, write the following...

    JAVA 6. Create two files and name them: puzzle.txt and puzzle2.txt Inside puzzle.txt, write the following text: MWTaahyiebt_e,c__hnyaoontuc;'e_rste_r_aynr_oert_e_gasoduoipdnp_got_shoeandtl__yty_oot_uhrree__apTdrH_oItgRhrDia_sml__eowtnotere.kr_ss_. Inside puzzle2.txt, write the following text: WTTohhriikssi__niigss,___ttbhhueet___wryrioogunh'gtr__emm_eessshssoaawggieen__gff_rrtoohmme___sswaarmmoppnllgee_22o..nttexxstt Open a file specified by the user. This file will contain a bunch of characters. You should read in each character from the file, one character at a time. Display every third character on the screen. Throw the other characters away. There is a sample input file called puzzle.txt, containing a little message you can...

  • Your mission in this programming assignment is to create a Python program that will take an...

    Your mission in this programming assignment is to create a Python program that will take an input file, determine if the contents of the file contain email addresses and/or phone numbers, create an output file with any found email addresses and phone numbers, and then create an archive with the output file as its contents.   Tasks Your program is to accomplish the following: ‐ Welcome the user to the program ‐ Prompt for and get an input filename, a .txt...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT