What's the easiest way to count the number of newlines in a string that contains newlines that conform to the cross-platform newline pattern: '\r\n?|\n'
.
Say we're skipping white space, or white space plus some other characters in a buffer, but in the mean time we would like to increment the line count. I'm doing something like:
nlinePat = re.compile(r'\r\n?|\n')
wsPat = re.compile(r'[ \t\r\n]+') # skip (specific) white space chars
commaPat = re.compile(r'[ \t\r\n]*,[ \t\r\n]*') # skip comma and surrounding white space
#...
m1 = wsPat.match(buffer)
bufferPos += len(m1.group(0))
m2 = nlinePat.findall(m1.group(0))
nlineCounter += len(m2))
(For example: can the above be done using a single regex operation, I feel it's an overhead to skip newlines first then to count them)