如何在 Java 中使用带有 Scanner.useDelimiter 的定界符?
How do I use a delimiter with Scanner.useDelimiter in Java?
sc = new Scanner(new File(dataFile));
sc.useDelimiter(",|\r\n");
我不明白 delimiter 是如何工作的,谁能通俗地解释一下?
The scanner can also use delimiters other than whitespace.
来自 Scanner API 的简单示例:
String input = "1 fish 2 fish red fish blue fish";
// \s* means 0 or more repetitions of any whitespace character
// fish is the pattern to find
Scanner s = new Scanner(input).useDelimiter("\s*fish\s*");
System.out.println(s.nextInt()); // prints: 1
System.out.println(s.nextInt()); // prints: 2
System.out.println(s.next()); // prints: red
System.out.println(s.next()); // prints: blue
// don't forget to close the scanner!!
s.close();
重点是理解正则表达式(regex
) inside the Scanner::useDelimiter
. Find an useDelimiter
tutorial here.
从正则表达式开始 here you can find 一个很好的教程。
备注
abc… Letters
123… Digits
\d Any Digit
\D Any Non-digit character
. Any Character
\. Period
[abc] Only a, b, or c
[^abc] Not a, b, nor c
[a-z] Characters a to z
[0-9] Numbers 0 to 9
\w Any Alphanumeric character
\W Any Non-alphanumeric character
{m} m Repetitions
{m,n} m to n Repetitions
* Zero or more repetitions
+ One or more repetitions
? Optional character
\s Any Whitespace
\S Any Non-whitespace character
^…$ Starts and ends
(…) Capture Group
(a(bc)) Capture Sub-group
(.*) Capture all
(ab|cd) Matches ab or cd
对于 Scanner,默认分隔符是空白字符。
但是Scanner可以根据一组定界符[=38=来定义令牌开始和结束的位置], 可以通过两种方式指定:
- 使用扫描仪方法:useDelimiter(String pattern)
- 使用 Scanner 方法:useDelimiter(Pattern pattern) 其中 Pattern 是指定分隔符集的正则表达式。
因此 useDelimiter()
方法用于标记扫描仪输入,其行为类似于 StringTokenizer class,请查看这些教程以获取更多信息:
这是一个 Example:
public static void main(String[] args) {
// Initialize Scanner object
Scanner scan = new Scanner("Anna Mills/Female/18");
// initialize the string delimiter
scan.useDelimiter("/");
// Printing the tokenized Strings
while(scan.hasNext()){
System.out.println(scan.next());
}
// closing the scanner stream
scan.close();
}
打印此输出:
Anna Mills
Female
18
例如:
String myInput = null;
Scanner myscan = new Scanner(System.in).useDelimiter("\n");
System.out.println("Enter your input: ");
myInput = myscan.next();
System.out.println(myInput);
这样您就可以使用 Enter 作为分隔符。
因此,如果您输入:
Hello world (ENTER)
它将打印 'Hello World'.
sc = new Scanner(new File(dataFile));
sc.useDelimiter(",|\r\n");
我不明白 delimiter 是如何工作的,谁能通俗地解释一下?
The scanner can also use delimiters other than whitespace.
来自 Scanner API 的简单示例:
String input = "1 fish 2 fish red fish blue fish";
// \s* means 0 or more repetitions of any whitespace character
// fish is the pattern to find
Scanner s = new Scanner(input).useDelimiter("\s*fish\s*");
System.out.println(s.nextInt()); // prints: 1
System.out.println(s.nextInt()); // prints: 2
System.out.println(s.next()); // prints: red
System.out.println(s.next()); // prints: blue
// don't forget to close the scanner!!
s.close();
重点是理解正则表达式(regex
) inside the Scanner::useDelimiter
. Find an useDelimiter
tutorial here.
从正则表达式开始 here you can find 一个很好的教程。
备注
abc… Letters
123… Digits
\d Any Digit
\D Any Non-digit character
. Any Character
\. Period
[abc] Only a, b, or c
[^abc] Not a, b, nor c
[a-z] Characters a to z
[0-9] Numbers 0 to 9
\w Any Alphanumeric character
\W Any Non-alphanumeric character
{m} m Repetitions
{m,n} m to n Repetitions
* Zero or more repetitions
+ One or more repetitions
? Optional character
\s Any Whitespace
\S Any Non-whitespace character
^…$ Starts and ends
(…) Capture Group
(a(bc)) Capture Sub-group
(.*) Capture all
(ab|cd) Matches ab or cd
对于 Scanner,默认分隔符是空白字符。
但是Scanner可以根据一组定界符[=38=来定义令牌开始和结束的位置], 可以通过两种方式指定:
- 使用扫描仪方法:useDelimiter(String pattern)
- 使用 Scanner 方法:useDelimiter(Pattern pattern) 其中 Pattern 是指定分隔符集的正则表达式。
因此 useDelimiter()
方法用于标记扫描仪输入,其行为类似于 StringTokenizer class,请查看这些教程以获取更多信息:
这是一个 Example:
public static void main(String[] args) {
// Initialize Scanner object
Scanner scan = new Scanner("Anna Mills/Female/18");
// initialize the string delimiter
scan.useDelimiter("/");
// Printing the tokenized Strings
while(scan.hasNext()){
System.out.println(scan.next());
}
// closing the scanner stream
scan.close();
}
打印此输出:
Anna Mills
Female
18
例如:
String myInput = null;
Scanner myscan = new Scanner(System.in).useDelimiter("\n");
System.out.println("Enter your input: ");
myInput = myscan.next();
System.out.println(myInput);
这样您就可以使用 Enter 作为分隔符。
因此,如果您输入:
Hello world (ENTER)
它将打印 'Hello World'.