1

I have two text files with the contents as below :

file1.txt

Hello
Second Line
Text line
Final Line

file2.txt

Final Line
Text line
line 3 of file2
World

What I am trying to do is use the each line from file2.txt as pattern to match against each line from file1.txt and print the line from file1.txt against which the pattern matches.

So far I have the following code :

BufferedReader file1= new BufferedReader(new FileReader("C:\\Users\\Ahmed Ismail Khalid\\Desktop\\file1.txt"));
BufferedReader file2= new BufferedReader(new FileReader("C:\\Users\\Ahmed Ismail Khalid\\Desktop\\file2.txt"));

while((line = inputfile.readLine()) != null)
    {
        while((pattern = patternsfile.readLine()) != null)
        {
            System.out.println(line);


            Pattern r = Pattern.compile(pattern);
            System.out.println(r);

            Matcher m = r.matcher(line);

            if (m.find( ))
            {
                System.out.println("Line corresponding to pattern in input.txt : " + line);
            }

        line = inputfile.readLine();
        }

    }

The output that I should get is :

Line corresponding to pattern in file1.txt : Text line 3
Line corresponding to pattern in file1.txt : Final Line

However the output I am getting is as below :

Hello
Final Line
Second Line
Text line
Text line
line 3 of file2
Final Line
World

Additionally, when I run the following code :

while((line = file1.readLine()) != null)
        {
            System.out.println(line);
            while((pattern = file2.readLine()) != null)
            {         

                System.out.println(pattern);
            }

            System.out.println();
        }

The output I should get is

Hello
Final Line
Text line
line 3 of file2
World

Second Line
Final Line
Text line
line 3 of file2
World

Text Line
Final Line
Text line
line 3 of file2
World

Final Line
Final Line
Text line
line 3 of file2
World

However, the output I get is :

Hello
Final Line
Text line
line 3 of file2
World
Second Line
Text line
Final Line

It seems that when the first line from the file1.txt all the lines from the file2.txt are printed. However, after the second line from file1.txt is printed, the nested while loop does not execute and therefore, the lines from file2.txt are not printed.

I am just starting out with java and have absolutely no experience with it. Therefore any and all help would be appreciated.

Thanks

EDIT :

I know that the current code returns lines from file1.txt that contains a match from file2.txt somewhere in the line. However I how do modify the code such that I can find the closet string with difference of n letters. For example, suppose file1.txt has the lines :

Hello
Second Line
Text line
Final Line

and file2.txt has the following lines :

Final Linee
Text line
line 3 of file2
Helloo

And what I want is to return Final Line and Hello from file1.txt as they match Final Linee and Helloo from file2.txt.

9
  • @ArvindKumarAvinash I know how to mark an answer as accepted. However, currently, I cannot test any solutions as I don't have access to my computer (using via mobile). I will do so as soon as I can. Thank you Commented Jun 4, 2020 at 11:53
  • And what I want is to return Final Line and Hello from file1.txt as they match Final Linee and Helloo from file2.txt. - Why not Text line as well? My answer precisely meets the requirements of your edited question. Just copy and execute it and let me know if you expect something different. Commented Jun 4, 2020 at 18:16
  • @ArvindKumarAvinash Oh the Text Line was a typo. I forgot to mention it since it does match. Thanks for pointing it out. As for your answer, let me copy and execute and I will let you know Commented Jun 4, 2020 at 18:21
  • Just replace with System.out.println(matcher.group()); with System.out.println(lineFromFile1); in my answer. I had written matcher.group() for your original question. Feel free to comment in case of any issue/doubt. Commented Jun 4, 2020 at 19:30
  • @ArvindKumarAvinash Still the same output, only printing Text Line Commented Jun 4, 2020 at 19:45

3 Answers 3

1

It seems in the first case you are matching every line of one file only to the corresponding line of the other file, so you don't get any matches. In the second case the second BufferedReader just continues reading new line after the first loop instead of restarting. The following worked for me, there are other solutions.

public class Regex {
public static void main (String[] args) throws IOException{
    BufferedReader inputFile= new BufferedReader(new FileReader("file1.txt"));

    String line;
    String pattern;

    while((line = inputFile.readLine()) != null){
        System.out.println(line);
        BufferedReader patternsFile = new BufferedReader(new FileReader("file2.txt"));
        while ((pattern = patternsFile.readLine()) != null){

            Pattern r = Pattern.compile(pattern);
            System.out.println(r);

            Matcher m = r.matcher(line);

            if (m.find()){
                System.out.println("Line corresponding to pattern in file1.txt : " + line);
            }
        }
    }
}

}

You probably should also pay attention to case sensitiveness and white spaces. Reading the whole files first as suggested is probably better for small files and can become a problem with very large ones and my proposal is not the best solution, but you probably don't need to focus on this at this point.

Sign up to request clarification or add additional context in comments.

Comments

1

What I am trying to do is use the each line from file2.txt as pattern to match against each line from file1.txt and print the line from file1.txt against which the pattern matches.

Your approach is not correct. You need to store the lines from both the files into separate collections and then process the collections as per your requirement.

Do it as follows:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) throws IOException {
        // Store all lines from the files in separate List<String>
        List<String> allLinesOfFile1 = Files.readAllLines(Paths.get("file1.txt"));
        List<String> allLinesOfFile2 = Files.readAllLines(Paths.get("file2.txt"));

        // Use each line from file2.txt as the pattern to match against each line from
        // file1.txt and print the line from file1.txt against which the pattern matches.
        for (String lineFromFile2 : allLinesOfFile2) {
            Pattern pattern = Pattern.compile("(" + lineFromFile2 + ")");
            for (String lineFromFile1 : allLinesOfFile1) {
                Matcher matcher = pattern.matcher(lineFromFile1);
                while (matcher.find()) {
                    System.out.println(matcher.group());
                }
            }
        }
    }
}

Output:

Final Line
Text line

Comments

1

EDIT: Updated answer. I will try commenting on your code so you know what's wrong instead of answering straight. If you just want an answer, use other replies' code.

// removed the printlns, these were cluttering the code
while((line = inputfile.readLine()) != null)
{
    while((pattern = patternsfile.readLine()) != null)
    {   
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(line);

        if (m.find( ))
        {
            System.out.println("Line corresponding to pattern in input.txt : " + line);
        }

        // Following line is wrong, since you double up reading the line.
        // It's already being read in the outer loop
        //line = inputfile.readLine();
    }
}

Ok, look at the code. You have a loop:

    read first line form fileA
        iterate over all lines from fileB with first line from fileA

Now the outer loop tries to do this:

    read second line form fileA
        iterate over all lines from fileB with second line from fileA

but it can't! Lines from fileB were already exhausted at the first run of the loop! So it's just an empty loop.

You can see for yourself by inserting this printlns into your loops:

while((line = inputfile.readLine()) != null)
{
    while((pattern = patternsfile.readLine()) != null)
    {   
        System.out.println("Matching pattern '" + pattern + "' with line '" + line + "'");
    }
    System.out.println("Empty loop with line '" + line + "' because pattern reader is exhausted");
}

Run just this loop, no extra code in it is necessary to see what happens.

2 Comments

the pattern file is file2.txt. Also I missed a few lines of code while pasting the code due to reversing changes using Ctrl+Z. Anyways the code is updated
Thanks a lot. This was really helpful in understanding where I went wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.