In: Computer Science
: 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
}
}
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()];
}
}