在Java中打印包含特定模式的字符串

发布于 2024-08-26 02:56:59 字数 965 浏览 6 评论 0原文

我试图在 .csv 文件的一行中查找正则表达式,因此我最终可以将所有匹配项保存到另一个文件中,并丢失所有其他垃圾。

所以我的文件中的一行可能如下所示: MachineName,User,IP,VariableData,Location

VariableData 是我想要匹配的内容,如果有匹配,则打印该行。我为此使用了一种模式,因为我只需要 VariableData 的 10 个变体中的 3 个,并且在这 3 个变体中,它们的编号不同(例如“pc104、pccrt102、pccart65”)。

我正在尝试使用 Scanner 类来做到这一点,并尽可能保持简单,以便我能够理解它。这就是我的方向...(该模式并不完整,只需像这样进行测试)。

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){

    }


}

}

这段代码循环,我猜测 .hasNext() 方法正在重置自身。我对 Matcher 类进行了一些尝试,但只找到了一种匹配表达式但无法获取整行的方法。

我的另一个想法可能是以某种方式计算包含模式的行,然后返回并打印与计数相对应的行。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

郁金香雨 2024-09-02 02:56:59

这是 grep 的设计目的,但如果您想在 Java 中执行此操作,那么您可以使用这个 while 循环体:

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

正如其他人提到的,问题您的原始代码是它实际上并没有推进扫描仪。

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.

筱果果 2024-09-02 02:56:59

目前,您只是询问扫描器是否有另一个条目,并且实际上没有得到它(通过next()

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

瑾兮 2024-09-02 02:56:59

首先猜测为什么永远循环是 Scanner 类有两个方法:hasNextnext

hasNext 将测试是否有另一个匹配的标记,而不移动 Scanner 在文件中的位置。

next 将返回下一个标记,如果由于某种原因不起作用,则会抛出异常。

通常,您会看到两者的用法如下:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文