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!
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
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
Related
libphonenumber requires a phone number and a country name as a parameter
to verify a phone number.
PhoneNumber numberProto = phoneUtil.parse("044 668 18 00", "CH");
System.out.println(numberProto);
System.out.println("is valid: "+phoneUtil.isValidNumber(numberProto));
I can find phone numbers from a text document using regex, but not country name associated with that particular phone number.
Please, suggest me a way to find valid phone numbers from text.
Example input:
..some text.. first number +13478093374 ..some text..
some new text.. second number.. +91 774-5001-827 ..some text.
some new text.. third number.. 044 668 18 00 ..some text.
some new text.. forth number.. 020-2689-0455 ..some text.
so the respective output should be,
phoneUtil.parse("044 668 18 00", "CH") //valid
phoneUtil.parse("+91 774-5001-827", "IN") //valid
phoneUtil.parse("+13478093374", "US") //valid
phoneUtil.parse("020-2689-0455", "IN") //valid
Please suggest a algorithm to add country parameter.
You can use the regex like this:
"(\d\d\d)-(\d\d\d\d)-(\d\d\d)\s"
Then use the code like this:
Pattern pattern = Pattern.compile("(\\d\\d\\d)-(\\d\\d\\d\\d)-(\\d\\d\\d)\\s");
String phoneNumber = "";
String text = "..some text.. valid number 774-0000-827 ..some text..";
Matcher matcher = pattern.matcher(text);
if (matcher.find())
{
phoneNumber += matcher.group(1) + " ";
phoneNumber += matcher.group(2) + " ";
phoneNumber += matcher.group(3) + " ";
PhoneNumber numberProto = phoneUtil.parse(phoneNumber, "CH");
System.out.println(numberProto);
System.out.println("is valid: "+ phoneUtil.isValidNumber(numberProto));
}
I am trying to take a text file which has a list of peoples first and last names with age and rearrange it so the console output would go from 46 Richman, Mary A. to Mary A. Richman 46. However, in my attempt to do so I have ran into issues (shown below) and I don't understand exactly why they're occurring (it was much worse earlier).
I'd really appreciate the assistance!
Text File:
75 Fresco, Al
67 Dwyer, Barb
55 Turner, Paige
108 Peace, Warren
46 Richman, Mary A.
37 Ware, Crystal
83 Carr, Dusty
15 Sledd, Bob
64 Sutton, Oliver
70 Mellow, Marsha
29 Case, Justin
35 Time, Justin
8 Shorts, Jim
20 Morris, Hugh
25 Vader, Ella
76 Bird, Earl E.
My Code:
import java.io.*;
import java.util.*;
public class Ex2 {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("people.txt"));
while (input.hasNext()) { // Input == people.txt
String line = input.next().replace(",", "");
String firstName = input.next();
String lastName = input.next();
int age = input.nextInt();
System.out.println(firstName + lastName + age);
}
}
}
Bad Console Output: (How is it throwing an Unknown Source Error?)
Fresco,Al67
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Ex2.main(Ex2.java:11)
Target Console Output:
Al Fresco 75
Barb Dwyer 67
Paige Turner 55
Warren Peace 108
Mary A. Richman 46
Crystal Ware 37
Dusty Carr 83
Bob Sledd 15
Oliver Sutton 64
Marsha Mellow 70
Justin Case 29
Justin Time 35
Jim Shorts 8
Hugh Morris 20
Ella Vader 25
Earl E. Bird 76
This will make sure the first name includes the middle initial
while (input.hasNext())
{
String[] line = input.nextLine().replace(",", "").split("\\s+");
String age = line[0];
String lastName = line[1];
String firstName = "";
//take the rest of the input and add it to the last name
for(int i = 2; 2 < line.length && i < line.length; i++)
firstName += line[i] + " ";
System.out.println(firstName + lastName + " " + age);
}
You can avoid the issue and simplify the logic by actually reading with input.nextLine() as shown in the below code with comments:
while (input.hasNextLine()) {
String line = input.nextLine();//read next line
line = line.replace(",", "");//replace ,
line = line.replace(".", "");//replace .
String[] data = line.split(" ");//split with space and collect to array
//now, write the output derived from the split array
System.out.println(data[2] + " " + data[1] + " " + data[0]);
}
I have the same problem as in this link
but with multiple patterns. My regex is like:
Pattern word = Pattern.compile("([\w]+ [\d]+)|([\d]+ suite)|([\w]+ road)");
If my sample text is,
XYZ Road 123 Suite
My desire output is,
XYZ Road 123
123 suite
But am getting
XYZ Road 123
only.
Thanks in advance!
You could try the below regex which uses positive lookahead assertion.
(?=(\b\w+ Road \d+\b)|(\b\d+ suite\b))
DEMO
String s = "XYZ Road 123 Suite";
Matcher m = Pattern.compile("(?i)(?=(\\b\\w+ Road \\d+\\b)|(\\b\\d+ suite))").matcher(s);
while(m.find())
{
if(m.group(1) != null) System.out.println(m.group(1));
if(m.group(2) != null) System.out.println(m.group(2));
}
Output:
XYZ Road 123
123 Suite
(?=(\b[\w]+ [\d]+))|(?=(\b[\d]+ suite))|(?=(\b[\w]+ road))
Try this.See demo.Grab the captures.
https://regex101.com/r/dU7oN5/16
Use positive lookahead to avoid string being consumed.
Something like this, maybe?
Pattern p = Pattern.compile("([\\w ] Road) (\\d+) (Suite)");
Matcher m = p.matcher(input);
if(m.find) {
System.out.println(m.group(1) + " " + m.group(2));
System.out.println(m.group(2) + " " + m.group(3));
}
Assuming there is a file:
Virginia Tyler Taylor Wilson
Ohio Grant Hayes Garfield Harrison_B McKinley Taft Harding
Massachusetts Kennedy Bush_GHW
New_York VanBuren Fillmore Roosevelt_T Roosevelt_F
I need to sort it in order, and below is my code:
TreeMap<String, String> map = new TreeMap<String, String>();
while (infile1.ready()){
String line = infile1.readLine();
String s = line.substring(0, line.indexOf(" "));
String p = line.substring(line.indexOf(" "));
map.put(s, p);
}
for (String p : map.keySet()){
System.out.println(p + " " + map.get(p));
}
My output is:
Massachusetts Kennedy Bush_GHW
New_York VanBuren Fillmore Roosevelt_T Roosevelt_F
Ohio Grant Hayes Garfield Harrison_B McKinley Taft Harding
Virginia Tyler Taylor Wilson
The expected output is:
Massachusetts **Bush_GHW Kennedy**
New_York **Fillmore Roosevelt_F Roosevelt_T VanBuren**
Ohio **Garfield Grant Harding Harrison_B Hayes McKinley Taft**
Virginia **Taylor Tyler Wilson**
The only difference is the order in the value, where I bold it in the expected output.
Is there a fast way to sort String p and puts it into the map?
you can write in short way
Arrays.sort(line.substring(line.indexOf(" ")).split(" "));
try this
String[] ps = line.substring(line.indexOf(" ")).split(" ");
Arrays.sort(ps);
then try combine ps to p with " ";
Massachusetts **Bush_GHW Kennedy**
New_York **Fillmore Roosevelt_F Roosevelt_T VanBuren**
Ohio **Garfield Grant Harding Harrison_B Hayes McKinley Taft**
Virginia **Taylor Tyler Wilson**
In your above expected output how Bush Comes after mass..
So please explore output correctly.
I want to Parse the lines of a file Using parsingMethod
test.csv
Frank George,Henry,Mary / New York,123456
,Beta Charli,"Delta,Delta Echo
", 25/11/1964, 15/12/1964,"40,000,000.00",0.0975,2,"King, Lincoln ",Alpha
This is the way i read line
public static void main(String[] args) throws Exception {
File file = new File("C:\\Users\\test.csv");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line2;
while ((line2= reader.readLine()) !=null) {
String[] tab = parsingMethod(line2, ",");
for (String i : tab) {
System.out.println( i );
}
}
}
public static String[] parsingMethod(String line,String parser) {
List<String> liste = new LinkedList<String>();
String patternString ="(([^\"][^"+parser+ "]*)|\"([^\"]*)\")" +parser+"?";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher =pattern.matcher(line);
while (matcher.find()) {
if(matcher.group(2) != null){
liste.add(matcher.group(2).replace("\n","").trim());
}else if(matcher.group(3) != null){
liste.add(matcher.group(3).replace("\n","").trim());
}
}
String[] result = new String[liste.size()];
return liste.toArray(result);
}
}
Output :
Frank George
Henry
Mary / New York
123456
Beta Charli
Delta
Delta Echo
"
25/11/1964
15/12/1964
40,000,000.00
0.0975
2
King
Lincoln
"
Alpha
Delta
Delta Echo
I want to remove this " ,
Can any one help me to improve my Pattern.
Expected output
Frank George
Henry
Mary / New York
123456
Beta Charli
Delta
Delta Echo
25/11/1964
15/12/1964
40,000,000.00
0.0975
2
King
Lincoln
Alpha
Delta
Delta Echo
Output for line 3
25/11/1964
15/12/1964
40
000
000.00
0.0975
2
King
Lincoln
Your code didn't compile properly but that was caused by some of the " not being escaped.
But this should do the trick:
String patternString = "(?:^.,|)([^\"]*?|\".*?\")(?:,|$)";
Pattern pattern = Pattern.compile(patternString, Pattern.MULTILINE);
(?:^.,|) is a non capturing group that matches a single character at the start of the line
([^\"]*?|\".*?\") is a capturing group that either matches everything but " OR anything in between " "
(?:,|$) is a non capturing group that matches a end of the line or a comma.
Note: ^ and $ only work as stated when the pattern is compiled with the Pattern.MULTILINE flag
I can't reproduce your result but I'm thinking maybe you want to leave the quotes out of the second captured group, like this:
"(([^\"][^"+parser+ "]*)|\"([^\"]*))\"" +parser+"?"
Edit: Sorry, this won't work. Maybe you want to let any number of ^\" in the first group as well, like this: (([^,\"]*)|\"([^\"]*)\"),?
As i can see the lines are related so try this:
public static void main(String[] args) throws Exception {
File file = new File("C:\\Users\\test.csv");
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder line = new StringBuilder();
String lineRead;
while ((lineRead = reader.readLine()) != null) {
line.append(lineRead);
}
String[] tab = parsingMethod(line.toString());
for (String i : tab) {
System.out.println(i);
}
}
public static String[] parsingMethod(String line) {
List<String> liste = new LinkedList<String>();
String patternString = "(([^\"][^,]*)|\"([^\"]*)\"),?";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
if (matcher.group(2) != null) {
liste.add(matcher.group(2).replace("\n", "").trim());
} else if (matcher.group(3) != null) {
liste.add(matcher.group(3).replace("\n", "").trim());
}
}
String[] result = new String[liste.size()];
return liste.toArray(result);
}
Ouput:
Frank George
Henry
Mary / New York
123456
Beta Charli
Delta,Delta Echo
25/11/1964
15/12/1964
40,000,000.00
0.0975
2
King, Lincoln
Alpha
as Delta, Delta Echo is in a quotation this should appear in the same line ! like as King, Lincoln