user90150
user90150

Reputation:

python: how to find \r\n\r\n in one single search

I have to split the file content based on first occurrence of \r\n\r\n

I need flexibility such a way that the lines can just ends with \r\n\r\n or \n\n.

How to split the text?

Example added:

\===================FILE BEGIN==========================================
name: about

title: About

publish: True

order: -1

\r\n

\r\n


**example.com** is a example website provides the latest examples in organized way. 

Blah blah blah....
Blah blah blah....
Blah blah blah....
\===================File END==========================================

First portion is my header(in yaml format) used for file identity and second portion is markdown text.

Upvotes: 1

Views: 3336

Answers (2)

Philipp
Philipp

Reputation: 49842

The regex pattern "\r\n\r\n|\n\n" should work. Simply use re.split or Regex.split and set maxsplit=1.

Upvotes: 0

Alex Martelli
Alex Martelli

Reputation: 882231

import re

linend = re.compile(r'\r\n\r\n|\n\n')
s = 'an example\n\nstring\n\nhere'
print linend.split(s, 1)
s = 'another\r\n\r\nexample\r\n\r\nhere'
print linend.split(s, 1)

prints:

['an example', 'string\n\nhere']
['another', 'example\r\n\r\nhere']

as requested.

Upvotes: 2

Related Questions