Print a string that contains a certain pattern in Java
- by jjpotter
I am trying to find a regular expression within a line of a .csv file, so I can eventually save all the matches to another file, and lose all the other junk.
So a line in my file might look like:
MachineName,User,IP,VariableData,Location
The VariableData is what I want to match, and if there's a match, print the line. I am using a pattern for this because I only want 3 out of 10 of variations of VariableData, and out of those 3, they are numbered differently(example, "pc104, pccrt102, pccart65").
I am trying to do this using the Scanner Class and keeping it simple as possible so I can understand it. Here is where I was heading with this...(the pattern isn't complete, just have it like this for testing).
import java.io.File;
import java.util.Scanner;
import java.util.regex.Pattern;
public class pcv {
public static void main(String[] args) {
File myFile = new File("c:\\temp\\report.csv");
Pattern myPat = Pattern.compile("pc");
try{
Scanner myScan = new Scanner(myFile);
while(myScan.hasNext()){
if(myScan.hasNext(myPat)){
System.out.println("Test");
}
}
}catch(Exception e){
}
}
}
This code loops, im guessing the .hasNext() methods are resetting themselves. I've played around with the Matcher class a little bit, but only found a way to match the expression but not get the whole line.
My other throught was maybe somehow count the line that contains the pattern, then go back and print the line that corresponds to the counts.