我一直在解决一些黑客基本JAVA问题,其中一个问题陈述有以下代码:
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
scanner.close();
if(N%2!=0){
System.out.println("Weird");
}
else if(N%2==0 && N<=5 && N>=2){
System.out.println("Not Weird");
}
else if(N%2==0 && N<=20 && N>=6){
System.out.println("Weird");
}
else if(N%2==0 && N>20){
System.out.println("Not Weird");
}}}
你能解释一下"scanner.skip("(\r\n|\n\r\u2028\u2029\u0085)?")“是什么吗我知道它跳过了某些模式,但是括号中有什么?
发布于 2022-09-04 16:58:48
意思是跳过换行符。
\n、\r、\n\r、U+0085 U+2028 U+2029都是用作行分隔符的字符或字符序列。
\n is LF or Line Feed or NewLine
\r is CR or Carriage Return
\r\n CR+LF is used in Windows as newLine sequence
U+0085 NEL is the Unicode character for NExt Line
U+2028 is the Unicode character for Line Separator
U+2029 is the Unicode character for Paragraph Separator
所有这些都是用来用行来破坏文件的。您所看到的是一个Regex模式,用于在字符串中搜索。其内容如下:
If you meat \n\r
or |
one of the characters in the group **[\n \r \u2028 \u2029 \u0085]** (those are considered single characters to choose from)
? 0 or 1 times
Regex是一个非常强大的工具。如果您需要在字符串中搜索特定的模式,我建议您学习它。
https://stackoverflow.com/questions/73601112
复制相似问题