I have a text file that is multiple of lines of "(what I want to grab)","junk","junk","junk" separated by newlines. 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());
}
.matches any character once. You're missing a quantifier, such as*(any number of times),+(any >0 number of times) or{n,m}(between n and m times). Use by appending it after the token you want to modify, e.g..*"is not a regex special character, it needs to be escaped only because it's in a string literal.". I would strongly recommend parsing a CSV file with a CSV parser. First, you wouldn't need to load all the lines to memory. Second, you won't run into problems like this, and third, it's then very easy to get just the first field or just the nth field.