3

Code that i tried:

import java.io.*;
import java.util.regex.*;
public class All {
    public static void main(String[] args) {
        String input = "IT&&faculty.*";
        try {
            FileInputStream fstream = new FileInputStream("uu.txt");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            while ((strLine = br.readLine()) != null) {
                if (Pattern.matches(input, strLine)) {
                    Pattern p = Pattern.compile("'(.*?)'");
                    Matcher m = p.matcher(strLine);
                    while (m.find()) {
                        String b = m.group(1);
                        String c = b.toString() + ".*";
                        System.out.println(b);

                        if (Pattern.matches(c, strLine)) {
                            Pattern pat = Pattern.compile("<(.*?)>");
                            Matcher mat = pat.matcher(strLine);
                            while (mat.find()) {
                                System.out.println(m.group(1));

                            }
                        } else {
                            System.out.println("Not found");
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}

The contents of my text file are: \ indicates it is a newline

Input file:

IT&&faculty('Mousum handique'|'Abhijit biswas'|'Arnab paul'|'Bhagaban swain')
 Mousum handique(designation|address|phone number|'IT Assistant          professor'|<AUS staff quaters>|#5566778899#)
 Abhijit biswas(designation|address|phone number|'IT Assistant professor'|<AUW staff quaters>|#5566778891#)
Arnab paul(designation|address|phone number|'IT Assistant professor'|<AUE staff quaters>|#5566778890#)
Bhagaban swain(designation|address|phone number|'IT Assistant professor'|<AUW staff quarters>|#5566778892#)

it gives result -

Mousum handique
Not found
Abhijit Biswas
Not found 
Arnab Paul
Not found
Bhagaban swain
Not found

whereas the results i want is:

Mousum handique
AUS staff quaters
Abhijit Biswas
AUW staff quaters
Arnab Paul
AUE staff quaters
Bhagaban swain
AUW staff quaters

That is i want after 1st match when it gets Mousum handique from the file it should again search the file and where it gets line like Mousum handique it should print whatever within <> for that corresponding line. Please refer data of my text file to understand my question. Sorry if my question seems stupid but i m trying it a lot!

2 Answers 2

4

You don't need to use string.matches method just use Patttern and Matcher classes to extract the name which was at the start of the line and also the contents between <> on the same line itself.

String s =  "IT&&faculty('Mousum handique'|'Abhijit biswas'|'Arnab paul'|'Bhagaban swain')\n" + 
        " Mousum handique(designation|address|phone number|'IT Assistant           professor'|<AUS staff quaters>|#5566778899#)\n" + 
        " Abhijit biswas(designation|address|phone number|'IT Assistant professor'|<AUW staff quaters>|#5566778891#)\n" + 
        "Arnab paul(designation|address|phone number|'IT Assistant professor'|<AUE staff quaters>|#5566778890#)\n" + 
        "Bhagaban swain(designation|address|phone number|'IT Assistant professor'|<AUW staff quarters>|#5566778892#)";
Matcher m = Pattern.compile("(?m)^\\s*([^\\(]+)\\([^\\)]*\\|<([^>]*)>[^\\)]*\\)").matcher(s);
while(m.find())
{
    System.out.println(m.group(1));
    System.out.println(m.group(2));
} 

Output:

Mousum handique
AUS staff quaters
Abhijit biswas
AUW staff quaters
Arnab paul
AUE staff quaters
Bhagaban swain
AUW staff quarters

DEMO

Update:

Use this regex to get also the id number.

String s =  "IT&&faculty('Mousum handique'|'Abhijit biswas'|'Arnab 
paul'|'Bhagaban swain')\n" + 
                " Mousum handique(designation|address|phone number|'IT Assistant           professor'|<AUS staff quaters>|#5566778899#)\n" + 
                " Abhijit biswas(designation|address|phone number|'IT Assistant professor'|<AUW staff quaters>|#5566778891#)\n" + 
                "Arnab paul(designation|address|phone number|'IT Assistant professor'|<AUE staff quaters>|#5566778890#)\n" + 
                "Bhagaban swain(designation|address|phone number|'IT Assistant professor'|<AUW staff quarters>|#5566778892#)";
        Matcher m = Pattern.compile("(?m)^\\s*([^\\(]+)\\([^\\)]*\\|<([^>]*)>[^\\)]*\\|#([^#]*)#[^\\)]*\\)").matcher(s);
        while(m.find())
        {
            System.out.println(m.group(1));
            System.out.println(m.group(2));
            System.out.println(m.group(3));
        }

Output:

Mousum handique
AUS staff quaters
5566778899
Abhijit biswas
AUW staff quaters
5566778891
Arnab paul
AUE staff quaters
5566778890
Bhagaban swain
AUW staff quarters
5566778892
Sign up to request clarification or add additional context in comments.

19 Comments

Thans for helping but i cant take the data as a string i HAVE to take it in a text file and retrieve data from there only using regex . I have just given an instance that i require answer whatever within <..> after matching the corresponding name but sometimes after matching the name i may also need the one within #..#. Sorry, but plz help how to do using text file only
Problem is i cant strore the data of String s like that in a string i am to use a text file read it and then extract the data in first match if it finds name then in next match for that corresponding name it has to print <...> or #..# from the text file only. Plz help
you could apply the same regex on the text file itself. Just read the whole file and stored it in a variable. And then apply the above regex on that variable.
Is it not possible that way - that it first reads text file finds IT&&faculty(...) line after that for each name it searches match in the text file if it finds then for that corresponding line it prints whatever within <....> or #...#
Is it not possible that way - that it first reads text file finds IT&&faculty(...) line after that for each name it searches match in the text file if it finds then for that corresponding line it prints whatever within <....> or #...#
|
1

One bug is here:

while (mat.find()) {
    System.out.println(m.group(1)); // <-- you should use mat - not m!!!
}

Second bug is here:

if (Pattern.matches(c, strLine)) {

This if is never entered since the String c is the previous match + ".*". Remove this if condition and it'll work.

Fixed code:

    ...
    Pattern p = Pattern.compile("'(.*?)'");
    Matcher m = p.matcher(strLine);
    while (m.find()) {
        String b = m.group(1);
        System.out.println(b);            
        Pattern pat = Pattern.compile("<(.*?)>");
        Matcher mat = pat.matcher(strLine);
        while (mat.find()) {
            System.out.println(mat.group(1));

        }            
    }
    ... 

Running this code with the input:

"Abhijit biswas(designation|address|phone number|'IT Assistant professor'|<AUW staff quaters>|#5566778891#)

outputs:

IT Assistant professor
AUW staff quaters

2 Comments

This only resulted the names and did not give whatever within < > for each corresponding names
@SoumyasreeBiswas the example I was running with was: ""Abhijit biswas(designation|address|phone number|'IT Assistant professor'|<AUW staff quaters>|#5566778891#)". See the lasted section I just added to the answer.

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.