简体   繁体   中英

Regex to match lines in-between two specific lines, in Python

I am trying to use regex to parse out some lines from text read in from a file. I know this could be done by reading in the file, line-by-line, but I like the elegance in capturing all the relevant bits of info in a single regex match.

The example file contents:

---
title: a title
layout: page
---

here's some text
================

this will be blog post content.

I am trying to produce a regex match that will return 2 groups: the data in-between the "---" lines, and all of the data after the 2nd "---" line. Here is the regex string I have come up with, and I am having an issue with it:

re.match('---\n(.*?)\n---\n(.*)', content, re.S)

This seems to work well, except when dealing with unix vs windows line-endings. Is there a way to allow this regex to match a \\r if it's present, too? It works with the unix, which is just \\n I believe.

Also, if you think this regex could be improved, I'm open to suggestions.

行尾标记被认为是空格,因此您可以使用结构\\s+来匹配与平台无关的行尾(和其他空格)。

序列(\\r\\n|\\r|\\n)将匹配所有“普通”行的结尾(分别为Windows,旧Mac和* nix)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM