I would like to find out how to print a a certain string from an input file using a scanner. For the string to be printed the line must start with *star and the string must be surrounded by quotation marks, and be the next token and on the same line as *star ignoring white space of course.
Sample input text file: "test.txt"
this is a test
*star "variableX"
more testing
*star "variableY
much more
testing
*star next"variableZ"
Based on this sample input text the output should be only.
开发者_JAVA百科"variableX"
Here is part of my code:
Scanner scanner = new Scanner (new File ("test.txt"));
while (scanner.hasNextLine()){
if(scanner.hasNext("*star")) {
scanner.next();
if(scanner.hasNext()){
String test = scanner.next();
System.out.println(test);
}
But it's missing some key things. Help is much appreciated!
package so3947761;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
private static final Pattern RELEVANT_LINE_PATTERN =
Pattern.compile("^\\*star\\s+\"(.*)\"$");
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new File("test.txt"));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
Matcher m = RELEVANT_LINE_PATTERN.matcher(line);
if (m.matches()) {
String data = m.group(1);
System.out.println(data);
}
}
}
}
Some remarks:
- I'm using the Scanner only for splitting the input into lines.
- The real work is done by the regular expression pattern, which contains all the requirements that you stated.
- If you want to parse the text in quotes further (for example to replace
\n
with a newline), this cannot be done by the regular expression, so you have to explicitly define which sequences to replace. - You may want to add
\\s*
at the right of the^
(beginning of the line) and at the left of the$
(end of the line) if you want to allow whitespace at these places.
while (scanner.hasNextLine()){
String line = scanner.nextLine();
if(line.startsWith("*star")){
String[] split= line.split("\\s+"); //Split the string where there is whitespace
//No whitespace after *star
if ((split.length<2)){
System.exit(0);
}
String file= split[1];
String star=split[0];
String file1=file.trim(); //Removes whitespace
if(file1.startsWith("\"")&&(file1.endsWith("\""))){
System.out.println(file1);
}
}
This does the trick
public static void main(String[] args) throws FileNotFoundException
{
Scanner scanner = new Scanner (new File ("c:\\test.txt"));
while (scanner.hasNextLine()){
if(scanner.hasNext(Pattern.compile("\\*star"))) {
scanner.next();
System.out.println(scanner.nextLine());
return;
}
else
scanner.nextLine();
}
}
Edit: The quotation part was missing in the answer, did not remember to put it as I got the output even without it, but here it is a more robust solution:
Scanner scanner = new Scanner (new File ("c:\\new.txt"));
while (scanner.hasNextLine()){
if(scanner.hasNext(Pattern.compile("\\*star"))) {
scanner.next();
String s = scanner.nextLine();
Matcher m = Pattern.compile("\".+\"").matcher(s);
if(m.find())
{
System.out.println(m.group());
return;
}
}
else
scanner.nextLine();
}
精彩评论