Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain this line written in JAVA

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 303
Mayank Bist Avatar asked Aug 31 '18 08:08

Mayank Bist


2 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 130
Surya Prakash Singh Avatar answered Oct 12 '22 20:10

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 26
memo Avatar answered Oct 12 '22 22:10

memo