Reputation: 41
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!
Upvotes: 3
Views: 30395
Reputation: 53525
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
Upvotes: 1
Reputation: 174706
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
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
Upvotes: 4