Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); do? [duplicate]

In HACKERRANK this line of code occurs very frequently. I think this is to skip whitespaces but what does that "\r\u2028\u2029\u0085" thing mean

 scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
like image 775
Mayank Bist Avatar asked Aug 31 '18 08:08

Mayank Bist


6 Answers

Scanner.skip skips a input which matches the pattern, here the pattern is :-

(\r\n|[\n\r\u2028\u2029\u0085])?

  • ? matches exactly zero or one of the previous character.
  • | Alternative
  • [] Matches single character present in
  • \r matches a carriage return
  • \n newline

  • \u2028 matches the character with index 2018 base 16(8232 base 10 or 20050 base 8) case sensitive

  • \u2029 matches the character with index 2029 base 16(8233 base 10 or 20051 base 8) case sensitive
  • \u0085 matches the character with index 85 base 16(133 base 10 or 205 base 8) case sensitive

1st Alternative \r\n

  • \r matches a carriage return (ASCII 13)
  • \n matches a line-feed (newline) character (ASCII 10)

2nd Alternative [\n\r\u2028\u2029\u0085]

  • Match a single character present in the list below [\n\r\u2028\u2029\u0085]
  • \n matches a line-feed (newline) character (ASCII 10)
  • \r matches a carriage return (ASCII 13)
  • \u2028 matches the character with index 202816 (823210 or 200508) literally (case sensitive) LINE SEPARATOR
  • \u2029 matches the character with index 202916 (823310 or 200518) literally (case sensitive) PARAGRAPH SEPARATOR
  • \u0085 matches the character with index 8516 (13310 or 2058) literally (case sensitive) NEXT LINE
like image 146
Surya Prakash Singh Avatar answered Nov 16 '22 22:11

Surya Prakash Singh


Skip \r\n is for Windows.

The rest is standard \r=CR, \n=LF (see \r\n , \r , \n what is the difference between them?)

Then some Unicode special characters:

u2028 = LINE SEPARATOR (https://www.fileformat.info/info/unicode/char/2028/index.htm)

u2029 = PARAGRAPH SEPARATOR (http://www.fileformat.info/info/unicode/char/2029/index.htm)

u0085 = NEXT LINE (https://www.fileformat.info/info/unicode/char/0085/index.htm)

like image 42
memo Avatar answered Nov 16 '22 22:11

memo


OpenJDK's source code shows that nextLine() uses this regex for line separators:

private static final String LINE_SEPARATOR_PATTERN = "\r\n|[\n\r\u2028\u2029\u0085]";
  • \r\n is a Windows line ending.
  • \n is a UNIX line ending.
  • \r is a Macintosh (pre-OSX) line ending.
  • \u2028 is LINE SEPARATOR.
  • \u2029 is PARAGRAPH SEPARATOR.
  • \u0085 is NEXT LINE (NEL).
like image 22
Davide Avatar answered Nov 16 '22 23:11

Davide


The whole thing is a regex expression, so you could simply drop it into https://regexr.com or https://regex101.com/ and it will provided you with a full description of what each part of the regex means.

Here it is for you though:

(\r\n|[\n\r\u2028\u2029\u0085])? / gm

1st Capturing Group (\r\n|[\n\r\u2028\u2029\u0085])?

? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)

1st Alternative \r\n

\r matches a carriage return (ASCII 13)

\n matches a line-feed (newline) character (ASCII 10)

2nd Alternative [\n\r\u2028\u2029\u0085]

Match a single character present in the list below

[\n\r\u2028\u2029\u0085]

\n matches a line-feed (newline) character (ASCII 10)

\r matches a carriage return (ASCII 13)

\u2028 matches the character 
 with index 202816 (823210 or 200508) literally (case sensitive)

\u2029 matches the character 
 with index 202916 (823310 or 200518) literally (case sensitive)

\u0085 matches the character with index 8516 (13310 or 2058) literally (case sensitive)

Global pattern flags

g modifier: global. All matches (don't return after first match)

m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

As for scanner.skip this does (Scanner Pattern Tutorial):

The java.util.Scanner.skip(Pattern pattern) method skips input that matches the specified pattern, ignoring delimiters. This method will skip input if an anchored match of the specified pattern succeeds.If a match to the specified pattern is not found at the current position, then no input is skipped and a NoSuchElementException is thrown.

I would also recommend reading Alan Moore's answer on here RegEx in Java: how to deal with newline he talks about new ways in Java 1.8.

like image 45
Popeye Avatar answered Nov 17 '22 00:11

Popeye


 scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
  1. in Unix and all Unix-like systems, \n is the code for end-of-line, \r means nothing special
  2. as a consequence, in C and most languages that somehow copy it (even remotely), \n is the standard escape sequence for end of line (translated to/from OS-specific sequences as needed)
  3. in old Mac systems (pre-OS X), \r was the code for end-of-line instead in Windows (and many old OSs), the code for end of line is 2 characters, \r\n, in this order as a (surprising;-) consequence (harking back to OSs much older than Windows), \r\n is the standard line-termination for text formats on the Internet

u0085 NEXT LINE (NEL)

U2029 PARAGRAPH SEPARATOR

U2028 LINE SEPARATOR'

The whole logic behind this is to remove the extra space and extra new line when input is from scanner

like image 24
SarthAk Avatar answered Nov 16 '22 22:11

SarthAk


There's already a similar question here scanner.skip. It won't skip whitespaces since the unicode char for it is not present (u0020)

\r = CR (Carriage Return) // Used as a new line character in Mac OS before X

\n = LF (Line Feed) // Used as a new line character in Unix/Mac OS X

\r\n = CR + LF // Used as a new line character in Windows

u2028 = line separator

u2029 = paragraph separator

u0085 = next line

like image 38
Alessandro R Avatar answered Nov 16 '22 23:11

Alessandro R