在Java中打印包含特定模式的字符串
我试图在 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是
grep
的设计目的,但如果您想在 Java 中执行此操作,那么您可以使用这个while
循环体:正如其他人提到的,问题您的原始代码是它实际上并没有推进扫描仪。
This is something
grep
is designed for, but if you want to do it in Java, then you can use thiswhile
loop body instead:As others have mentioned, the problem with your original code is that it doesn't actually advance the
Scanner
.目前,您只是询问扫描器是否有另一个条目,并且实际上没有得到它(通过
next()
)At the moment you're only asking the scanner if it has another entry, and not actually getting it (via
next()
)首先猜测为什么永远循环是 Scanner 类有两个方法:
hasNext
和next
。hasNext
将测试是否有另一个匹配的标记,而不移动Scanner
在文件中的位置。next
将返回下一个标记,如果由于某种原因不起作用,则会抛出异常。通常,您会看到两者的用法如下:
First guess as to why you're looping forever is that the Scanner class has two methods:
hasNext
andnext
.hasNext
will test if there is another token which matches, without moving theScanner
'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: