I have been recommended the following line of code to use on a text file:
arr = str.split(/\n{2,}/).map { |s| s.split(/\n/) }
Am trying to understand how the:
(/\n{2,}/)
Part is working and exactly what it does.
I have been recommended the following line of code to use on a text file:
arr = str.split(/\n{2,}/).map { |s| s.split(/\n/) }
Am trying to understand how the:
(/\n{2,}/)
Part is working and exactly what it does.
The leading and trailing /
mark the beginning and the end of a regular expression. \n
will match any single newline. {2,}
after a symbol (in this case \n
) will match any occurrence of the symbol repeated two or more times, in this case two or more consecutive newlines. Had it been \n{3,6}
, it would match any consecutive newlines repeated between 3 and 6 times.