开发者

Print a string that contains a certain pattern in Java

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.


This is something grep is designed for, but if you want to do it in Java, then you can use this while loop body instead:

while(myScan.hasNext()){
   String line = myScan.nextLine();
   if (myPat.matcher(line).find()) {
      System.out.println(line);
   }
}

As others have mentioned, the problem with your original code is that it doesn't actually advance the Scanner.


At the moment you're only asking the scanner if it has another entry, and not actually getting it (via next())


First guess as to why you're looping forever is that the Scanner class has two methods: hasNext and next.

hasNext will test if there is another token which matches, without moving the Scanner's position in the file.

next will return the next token, and throw an exception if it doesn't work for some reason.

So usually, you'll see the two used like so:

String token = "";
if(myScan.hasNext(wtv)) {
    token = myScan.next(wtv);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜