Listing with new lowercase characters

I have a line as shown below:

s = 'hello\n this is\n a forum\n'

And I use regex to get every single / double occurrence of each character. I want to create a list likeli = [ 'h','e','ll','o','\n','t','h'....]

I used

pattern = re.compile(r'(.)\1?' , re.IGNORECASE)
newList = [m.group() for m in pattern.finditer(s)]
print newList

But it gave me newList= [ 'h','e','ll','o','t','h'....], here I could not get the character "\ n" of a new line. How do I change my patter to get "\ n" in my list?

+4
source share
1 answer

Use re.DOTALLas

pattern = re.compile(r'(.)\1?' , re.IGNORECASE | re.DOTALL)

Quote from the docs,

Make a '.' a special character matches any character in general, including a new line; without this flag. "will match anything but a new line.

itertools.groupby

from itertools import groupby
print ["".join(grp) for char, grp in groupby(s)]
+4

Source: https://habr.com/ru/post/1529721/


All Articles