Question

In: Computer Science

: Create a Java program that will accept a regular expression and a filename for a...

: 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 need 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]

NOTE: You do not have to implement every possible regular expression. Only the 3 above are needed.

Input: Your program should take two command line arguments. The first should be the regular exception, the second should be the filename.

Output: The output from your program should be formatted exactly 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 cannot 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. Therefore, you cannot use any pattern matching methods in any programming language.

Code:

import java.util.*;
import java.io.*;
class Regex
{
public static void main(String args[]) throws Exception {
String pattern = args[0];
BufferedReader br = new BufferedReader(new FileReader(args[1]));
String line;
boolean flag = false, flag2 = false;
int lineNumber = 1;
while((line = br.readLine()) != null){
//here ist the error
flag = findMatches(line, pattern, lineNumber);
if(flag == true)
{
// once when the item was found , the flag2 will be true. so now we can
// Output a message if there was no items found
flag2 = true;
}

lineNumber++;
}
if(flag2 == false)
{
System.out.println("No match was found");
}
}

public static boolean findMatches(String st, String p, int lineNumber) {
int n = st.length();
String temp;
boolean flag = false;
for(int i=0;i<=n;i++){
for(int j=i+1;j<=n;j++) {
temp = st.substring(i, j);
if(isMatch(temp, p)){
flag = true;
System.out.println("Match found on line " + lineNumber + ", sttarting at postition " + (i+1) + " and ending at postition " + j + ": " + temp);
  
}
}
}
return flag;

}

public static boolean isMatch(String st, String p){

//fill this out

}
}

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

import java.io.*;
import java.util.*;

class Regex {

  public static void main(String args[])throws Exception {
    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 static 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 static 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()];
  }
}





Related Solutions

create a regular expression in Java that defines the following. A "Stir" is a right brace...
create a regular expression in Java that defines the following. A "Stir" is a right brace '}' followed by zero or more lowercase letters 'a' through 'z' and decimal digits '0' through '9', followed by a left parenthesis '('.
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression from the user, and then prints every line that matches the RE.
Define a regular expression in JAVA for the following An "Ent" is an at sign '@',...
Define a regular expression in JAVA for the following An "Ent" is an at sign '@', followed by one or more decimal digits '0' through '9' or uppercase letters 'R' through 'W', followed by an octothorpe sign '#'
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list...
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list of integers from use input and has the following methods: 1. min - Finds the smallest integer in the list. 2. max- Finds the largest integer in the list. 3. average - Computes the average of all the numbers. 4. main - method prompts the user for the inputs and ends when the user enter Q or q and then neatly outputs the entire...
C# Create a console application that prompts the user to enter a regular expression, and then...
C# Create a console application that prompts the user to enter a regular expression, and then prompts the user to enter some input and compare the two for a match until the user presses Esc: The default regular expression checks for at least one digit. Enter a regular expression (or press ENTER to use the default): ^[a- z]+$ Enter some input: apples apples matches ^[a-z]+$? True Press ESC to end or any key to try again. Enter a regular expression...
Using Java 8. Write a program that reads an expression in postfix notation, builds the expression...
Using Java 8. Write a program that reads an expression in postfix notation, builds the expression tree and prints the expression in prefix and infix notation and evaluates the expression. (Hint use a stack)
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
1. What is a regular expression? Write a regular expression that will detect “College” and “collegE”....
1. What is a regular expression? Write a regular expression that will detect “College” and “collegE”. 2. What is degree centrality? Create a graph of 4 vertices and compute the degree centrality of the vertices. 3. Compute internal and external community densities for a graph containing 6 nodes. You can create any graph of 6 nodes with at least 4 edges.
C# & ASP.NET Create a console application that prompts the user to enter a regular expression,...
C# & ASP.NET Create a console application that prompts the user to enter a regular expression, and then prompts the user to enter some input and compare the two for a match until the user presses Esc: The default regular expression checks for at least one digit. Enter a regular expression (or press ENTER to use the default): ^[a- z]+$ Enter some input: apples apples matches ^[a-z]+$? True Press ESC to end or any key to try again. Enter a...
Create and test a python regular expression that matches a street address consisting of a number...
Create and test a python regular expression that matches a street address consisting of a number with one or more digits followed by two words of one or more characters each. The tokens should be separated by one space each, as in 123 Main Street.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT