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.
create a program in java that will evaluate a logical expression (compound proposition) based on the...
create a program in java that will evaluate a logical expression (compound proposition) based on the given truth values of individual propositional variables. The logical expression may include the logical AND or the logical OR operators. The NOT operator will be included in the variable names itself. So, a proposition such as ¬a would appear as na in the logical expression. Here is an example of a logical expression using proper notation: a ∧ b ∨ ¬c ∨ d This...
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 '#'
PYTHON Write a regular expression that will accept any properly formatted email address, and reject any...
PYTHON Write a regular expression that will accept any properly formatted email address, and reject any invalid email address. An example of a valid inputted email address would be "[email protected]".
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...
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...
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...
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.
create a c++ proram to accept a positive number in the main program. Create and call...
create a c++ proram to accept a positive number in the main program. Create and call the following functions 1. 1. OddorEven -to determine if the number is an odd or even number ( do not use $ or modul opeartor) and will return " even number " or " odd number ". 2. Factorial - to compute the factorial of N. 3. Power - to compute the power of N without using pow function. display the result in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT