Python Regex,如何使用Regex获得一定数量的文本行,直到某个单词?

Python Regex,如何使用Regex获得一定数量的文本行,直到某个单词?,python,regex,parsing,Python,Regex,Parsing,我试着把课程和课程号与先决条件一起拉出来。然而,我很难只获得课程(例如:ACCT 203),达到并包括先决条件(例如:ACCT 301),其他什么都没有。我正在用python做这件事,希望以后能将这些数据插入数据库。有人能帮忙吗?我对regex比较陌生 ACCT 203 Financial Accounting Three Credits Development of basic accounting concepts. Emphasis is on the classifying, re

我试着把课程和课程号与先决条件一起拉出来。然而,我很难只获得课程(例如:ACCT 203),达到并包括先决条件(例如:ACCT 301),其他什么都没有。我正在用python做这件事,希望以后能将这些数据插入数据库。有人能帮忙吗?我对regex比较陌生

ACCT 203 Financial Accounting Three Credits Development of basic accounting concepts. Emphasis is on the classifying, recording, and reporting of business transactions for all forms of business organizations. Offered every semester. ACCT 204 Managerial Accounting Three Credits Emphasis is on generating, analyzing, and using accounting information in the planning and control processes. Topics include budgets, standards, cost systems, incremental analysis, and ~nancial statement analysis. Offered every semester. Prerequisite: ACCT 203 ACCT 301 Intermediate Accounting I Three Credits This is the ~rst course in a two-course sequence that is intended to provide a comprehensive understanding of the concepts, principles, assumptions, and conventions that are used for classifying, recording, and reporting economic transactions for a business entity. Offered every fall. Prerequisite: ACCT 204 or permission of instructor ACCT 302 Intermediate Accounting II Three Credits This is the second course in a two-course sequence that is intended to provide a comprehensive understanding of the concepts, principles, assumptions, and conventions that are used for classifying, recording, and reporting economic transactions for a business entity. Offered every spring. Prerequisite: ACCT 301 or permission of instructor ACCT 303 Accounting Theory and Practice Three Credits This course is intended to provide an understanding of items that present measurement and reporting problems for the accountant. It will also discuss current issues that the accounting profession is attempting to establish and guidelines for their measurement and reporting. Prerequisite: ACCT 302 ACCT 310 会计203 财务会计三学分 基本会计概念的发展。重点是分类,, 记录和报告所有形式的业务交易 组织。每学期提供。 会计科目204 管理会计 三个学分 重点是生成、分析和使用会计信息 规划和控制过程。主题包括预算、标准、成本体系、, 增量分析和财务报表分析。每学期提供。 先决条件: 会计203 会计301 中级会计I 三个学分 这是两个课程序列中的第一个课程,旨在提供 全面理解概念、原则、假设和 用于分类、记录和报告经济数据的约定 业务实体的事务处理。每年秋天都有。 先决条件: 会计204或讲师的许可 会计302 中级会计2 三个学分 这是两个课程序列中的第二个课程,旨在提供 全面理解概念、原则、假设和 用于分类、记录和报告经济数据的约定 业务实体的事务处理。每年春天提供。 先决条件: ACCT 301或讲师的许可 会计科目303 会计理论与实务 三个学分 本课程旨在让学生了解所呈现的项目 会计的计量和报告问题。它还将讨论 会计行业正在试图建立和解决的当前问题 测量和报告指南。 先决条件: 会计302 会计科目310
我不确定这是否正是你想要的。但这就是我的解决方案

>>> classes = re.findall("[A-Z][A-Z][A-Z][A-Z] [0-9][0-9][0-9]", text)  
>>> for i in classes:             #just find element by order
...     print(i)
...
ACCT 203
ACCT 204
ACCT 203
ACCT 301
ACCT 204
ACCT 302
ACCT 301
ACCT 303
ACCT 302
ACCT 310

正则表达式不是那么容易,甚至对我来说也不是(我已经在Py编程领域工作了几年)。我建议从其他主题开始…为什么您认为正则表达式是一个解决方案?我已经尝试过([a-Z]{3,4}\s\d\d\d)\d+[a-Z]{3,4}以及其他基本主题,如\d+\s\d\d\d@bah还有什么其他选项@cdarkef例如,
str.startswith('ACCT')