首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Regex将行以引号开头组合

使用Regex将行以引号开头组合
EN

Stack Overflow用户
提问于 2022-11-20 12:11:12
回答 1查看 25关注 0票数 0

我想将两行与只有一行提要\n结合起来,下一行有时以引号开头。我正在尝试使用这段代码将它们组合起来,并使用\"查找引号,

代码语言:javascript
运行
复制
comb_nextline = re.sub(r'(?<=[^\.][A-Za-z,-])\n[ ]*(?=[a-zA-Z0-9\(\"])', ' ', txt)

但它不适用于以引号开头的行。有什么方法将以引号开头的行组合起来吗?谢谢!

我的txt看起来是这样的:

代码语言:javascript
运行
复制
import re
 
txt= '''
The first process, called wafer bumping, involves a reflow solder process to form the solder balls on all of the input/output
(I/O) pads on the wafer. Because of the extremely small geometries involved, in some instances this process is best accomplished in a hydrogen atmosphere. RTC offers a high temperature furnace for this application, equipped with the hydrogen package, providing a re-flow process in a 100 hydrogen atmosphere. For a second process, called 
"chip joining", RTC offers both a near infrared or forced convection oven.
'''

comb_nextline = re.sub(r'(?<=[^\.][A-Za-z,-])\n[ ]*(?=[a-zA-Z0-9\(\"])', ' ', txt)
print(comb_nextline)

我希望能得到这个

代码语言:javascript
运行
复制
txt = 
'''
The first process, called wafer bumping, involves a reflow solder process to form the solder balls on all of the input/output (I/O) pads on the wafer. Because of the extremely small geometries involved, in some instances this process is best accomplished in a hydrogen atmosphere. RTC offers a high temperature furnace for this application, equipped with the hydrogen package, providing a re-flow process in a 100 hydrogen atmosphere. For a second process, called "chip joining", RTC offers both a near infrared or forced convection oven.
'''
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-20 12:15:45

还可以在匹配换行符之前匹配可选空格。

代码语言:javascript
运行
复制
(?<=[^.][A-Za-z,-]) *\n *(?=[a-zA-Z0-9(\"])

Regex演示 x- Python演示

或使用否定式字符类[^\S\n]匹配所有没有换行符的空格。

代码语言:javascript
运行
复制
(?<=[^.][A-Za-z,-])[^\S\n]*\n[^\S\n]*(?=[a-zA-Z0-9(\"])

Regex演示

代码语言:javascript
运行
复制
import re

txt = '''
The first process, called wafer bumping, involves a reflow solder process to form the solder balls on all of the input/output
(I/O) pads on the wafer. Because of the extremely small geometries involved, in some instances this process is best accomplished in a hydrogen atmosphere. RTC offers a high temperature furnace for this application, equipped with the hydrogen package, providing a re-flow process in a 100 hydrogen atmosphere. For a second process, called 
"chip joining", RTC offers both a near infrared or forced convection oven.
'''

comb_nextline = re.sub(r'(?<=[^.][A-Za-z,-]) *\n *(?=[a-zA-Z0-9(\"])', ' ', txt)
print(comb_nextline)

输出

代码语言:javascript
运行
复制
The first process, called wafer bumping, involves a reflow solder process to form the solder balls on all of the input/output (I/O) pads on the wafer. Because of the extremely small geometries involved, in some instances this process is best accomplished in a hydrogen atmosphere. RTC offers a high temperature furnace for this application, equipped with the hydrogen package, providing a re-flow process in a 100 hydrogen atmosphere. For a second process, called "chip joining", RTC offers both a near infrared or forced convection oven.
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74507967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档