I'm trying to read input word by word, but couldn't figure out how to set Scanner's delimiter to whitespace and punctuation marks except ' (the single quote). Here's what I got
我试着逐字逐句地读输入,但不知道如何将扫描器的分隔符设置为空格和标点符号,除了'(单引号)。这就是我得到的
BufferedReader input;
String line;
Scanner sc;
String word;
try {
input = new BufferedReader(new FileReader(path));
while (input.ready()) {
line = input.readLine();
System.out.println("Current Line: " + line);
sc = new Scanner(line);
sc.useDelimiter("\\W\\s^\'");
//...
}
}
//...
2 个解决方案
#1
2
I assume you mean?
我认为你的意思吗?
sc.useDelimiter("\\W\\s^\'");
I would use
我将使用
sc.useDelimiter("[^\\w']+");
String line= "Hello, world!\n 'Computer\n \n Science'\n Hell\n";
System.out.println(Arrays.toString(line.split("[^\\w']+")));
prints
打印
[Hello, world, 'Computer, Science', Hell]
String line= "Hello, world!\n 'Computer\n \n Science'\n Hell\n";
Scanner scan = new Scanner(line);
scan.useDelimiter("[^\\w']+");
while(scan.hasNext())
System.out.print("|"+scan.next());
System.out.println("|");
prints
打印
|Hello|world|'Computer|Science'|Hell|
#2
1
You can also use the Tokenizer like that:
你也可以用这样的记号笔:
StringTokenizer st1 = new StringTokenizer("a|b|c");
while(st1.hasMoreTokens())
System.out.println(st1.nextToken());
Hope that could help you in your case.
希望这能对你的情况有所帮助。
#1
2
I assume you mean?
我认为你的意思吗?
sc.useDelimiter("\\W\\s^\'");
I would use
我将使用
sc.useDelimiter("[^\\w']+");
String line= "Hello, world!\n 'Computer\n \n Science'\n Hell\n";
System.out.println(Arrays.toString(line.split("[^\\w']+")));
prints
打印
[Hello, world, 'Computer, Science', Hell]
String line= "Hello, world!\n 'Computer\n \n Science'\n Hell\n";
Scanner scan = new Scanner(line);
scan.useDelimiter("[^\\w']+");
while(scan.hasNext())
System.out.print("|"+scan.next());
System.out.println("|");
prints
打印
|Hello|world|'Computer|Science'|Hell|
#2
1
You can also use the Tokenizer like that:
你也可以用这样的记号笔:
StringTokenizer st1 = new StringTokenizer("a|b|c");
while(st1.hasMoreTokens())
System.out.println(st1.nextToken());
Hope that could help you in your case.
希望这能对你的情况有所帮助。