python - 如何使用正则表达式从文本中构建 python 列表?

标签 python python-3.x regex

我有以下一堆文字:

text = """SECTION 1. CHAPTER 1. Chapter title. Art. 1.- Lorem ipsum, blah, blah. Art 2.- More meaningless text. Art 3.- A little more text. CHAPTER 2. Another chapter. Art 4.- Lorem ipsum blah, blah, blah. Art. 5.- It's getting boring. SECTION 2. CHAPTER 1. Another chapter in another section. Art. 6.- The last text. SECTION 3. CHAPTER 1. Another chapter in another section. Art. 6.- The last text. SECTION 4. CHAPTER 1. Another chapter in another section. Art. 6.- The last text."""

我想拆分如下:

RE = r'(SECTION.*?SECTION)'
m = re.findall(RE, text, re.DOTALL)
sections = []
if m:
   for match in m:
        sections.append(match)

希望它会产生一个包含 4 个元素的列表,但我最终只有 2 个元素。

['SECTION 1. .....', 'SECTION 3. .....']  # only showing the first letters of each element

之后,我想对章节文章做同样的事情。

有什么想法吗?

最佳答案

假设单词 SECTION 仅在字符串中有新的“section”时出现,您始终可以使用默认的 .split 方法,这样更容易比使用正则表达式。

这是一个例子:

text = """SECTION 1. CHAPTER 1. Chapter title. Art. 1.- Lorem ipsum, blah, blah. Art 2.- More meaningless text. Art 3.- A little more text. CHAPTER 2. Another chapter. Art 4.- Lorem ipsum blah, blah, blah. Art. 5.- It's getting boring. SECTION 2. CHAPTER 1. Another chapter in another section. Art. 6.- The last text. SECTION 3. CHAPTER 1. Another chapter in another section. Art. 6.- The last text. SECTION 4. CHAPTER 1. Another chapter in another section. Art. 6.- The last text."""

delimiter = 'SECTION'
sections = [delimiter + s for s in text.split(delimiter)[1:]]

结果将是:

>>> sections
['SECTION 1. ...', 'SECTION 2. ...', 'SECTION 3. ...', 'SECTION 4. ...']

关于python - 如何使用正则表达式从文本中构建 python 列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33976539/

相关文章:

python - 如何为我的 tilemap 中的图 block 赋予 sprite?

python - 使用 python 或 bash 按时间戳将音频文件复制到新文件夹

javascript - 如何让这个 websocket 示例与 Flask 一起工作?

python - 如何将嵌套的字典列表展平为多行?

Java正则表达式,数字,加号,减号,井号

python - django模板中的模拟方法调用

python - PyAudio 包未安装

python - 无法在 python 列表中追加元素

php - 在 PHP 中解析 Javascript 文件以提取其中定义的数组的最佳方法

java - JSF <f :validateRegex pattern