[英]Using java regex to get certain part of each line of a text file
I have a text file that is multiple of lines of "(what I want to grab)"
, "junk"
, "junk"
, "junk"
separated by newlines. 我有一个文本文件,其中包含多行以换行符分隔的
"(what I want to grab)"
, "junk"
, "junk"
, "junk"
。 I'm reading the file into a list of strings and trying to use regex to print out what I want to grab, but I cannot seem to get this to work. 我正在将文件读入字符串列表,并尝试使用正则表达式打印出我想获取的内容,但似乎无法正常工作。
The way I understand regex, ^
matches the start of a new line, \\"
matches to the first quotation after ^
, .
matches anything, then \\"
matches the next quotation. 我了解正则表达式的方式是,
^
匹配新行的开头, \\"
匹配^
后的第一个引号, .
匹配任何内容,然后\\"
匹配下一个引号。 What am I missing? 我想念什么?
List<String> result = Files.readAllLines(Paths.get("file.txt"));
Pattern pattern = Pattern.compile("^\".\"");
for (int i = 0; i < result.size(); i++)
{
System.out.println(result.get(i));
Matcher matcher = pattern.matcher(result.get(i));
System.out.println(matcher.find());
}
Here's a simple regex which should solve your problem: 这是一个简单的正则表达式,可以解决您的问题:
String regex = "(^[\"][^\"]+[\"])";
This will match the beginning of the line, then directly afterwards it will match one single quotation mark. 这将与行的开头匹配,然后直接与之匹配一个单引号。 Then it'll match anything except a quotation mark until it reaches one.
然后,它将匹配除引号之外的所有内容,直到达到一个为止。
Another (possibly more legible) version from Aaron in the comments. 评论中Aaron的另一个版本(可能更清晰)。
^\"[^\"]+\"
Choose which you prefer. 选择您喜欢的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.