You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Regex合并符合特定条件的两行文本?

如何用正则表达式合并特定两行文本?

需要实现的逻辑:当某一行仅包含单个单词(单词可包含逗号,或连字符-),且该行末尾是换行符时,将该行与下一行合并为一行,中间用空格分隔。

原文本示例

import re

text = '''
Critical
Accounting Policies and Estimates 
Review,
Approval or Ratification of Transactions with Related Persons 

Audit-Related
Fees are fees for assurance and related services by the principal accountant that are traditionally performed by the principal accountant and which are reasonably related to the performance of the audit or review of the registrant s financial statements and fees attributed to the audit of Guskin Gold Corporation, our wholly owned subsidiary. 

Effective risk oversight is an important priority of the Board of Directors. Because risks are considered in virtually every business decision, the Board of Directors discusses risk throughout the year generally or in connection with specific proposed actions. The Board of Directors approach to risk oversight includes understanding the critical risks in the Company s business and strategy, evaluating the Company s risk management processes, allocating responsibilities for risk oversight among the full Board of Directors, and fostering an appropriate culture of integrity and compliance with legal responsibilities. 

Corporate
Governance 

The
Company promotes accountability for adherence to honest and ethical conduct; endeavors to provide full, fair, accurate, timely and understandable disclosure in reports and documents that the Company files with the SEC and in other public communications made by the Company; and strives to be compliant with applicable governmental laws, rules and regulations. The Company has not formally adopted a written code of business conduct and ethics that governs the Company s employees, officers and Directors as the Company is not required to do so. 
'''

尝试的无效代码

combine = re.sub(r'((?=[A-Za-z,-])\n(?=[a-zA-Z]))', ' ', text) 

正确的正则实现

你需要锚定行的首尾来确保目标行只有单个单词,正确的正则写法如下:

combine = re.sub(r'^([A-Za-z,-]+)\n', r'\1 ', text, flags=re.MULTILINE)

正则规则说明

  • ^:在re.MULTILINE模式下,匹配每一行的开头
  • ([A-Za-z,-]+):捕获组,匹配由字母、逗号、连字符组成的单个单词(+确保至少有一个字符)
  • \n:匹配单词后的换行符
  • r'\1 ':替换为捕获到的单词,后跟一个空格,实现两行合并
  • flags=re.MULTILINE:开启多行模式,让^能匹配每一行的开头,而非整个文本的开头

最终效果

运行上述代码后,文本将符合预期:

text = '''
Critical Accounting Policies and Estimates 
Review, Approval or Ratification of Transactions with Related Persons 

Audit-Related Fees are fees for assurance and related services by the principal accountant that are traditionally performed by the principal accountant and which are reasonably related to the performance of the audit or review of the registrant s financial statements and fees attributed to the audit of Guskin Gold Corporation, our wholly owned subsidiary. 

Effective risk oversight is an important priority of the Board of Directors. Because risks are considered in virtually every business decision, the Board of Directors discusses risk throughout the year generally or in connection with specific proposed actions. The Board of Directors approach to risk oversight includes understanding the critical risks in the Company s business and strategy, evaluating the Company s risk management processes, allocating responsibilities for risk oversight among the full Board of Directors, and fostering an appropriate culture of integrity and compliance with legal responsibilities. 

Corporate Governance 

The Company promotes accountability for adherence to honest and ethical conduct; endeavors to provide full, fair, accurate, timely and understandable disclosure in reports and documents that the Company files with the SEC and in other public communications made by the Company; and strives to be compliant with applicable governmental laws, rules and regulations. The Company has not formally adopted a written code of business conduct and ethics that governs the Company s employees, officers and Directors as the Company is not required to do so. 
'''

内容的提问来源于Stack Exchange,提问作者Hello World

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 02:10:31