Using Regex to combine lines start with quotation marks

Question:

I would like to combine two lines with only one line feed n, and sometime the next line starts with a quotation mark. I am trying use this code to combine them, with " to find quotation marks,

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

but it doesn’t work with the line start with a quotation mark. Is there any way to combine lines starts with quotation marks? Thanks!

My txt looks like this:

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)

And I hope to get this

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.
'''
Asked By: Hello World

||

Answers:

You can also match optional spaces before matching the newline

(?<=[^.][A-Za-z,-]) *n *(?=[a-zA-Z0-9("])

Regex demo | Python demo

Or matching all spaces without newlines using a negated character class [^Sn]

(?<=[^.][A-Za-z,-])[^Sn]*n[^Sn]*(?=[a-zA-Z0-9("])

Regex demo

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)

Output

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.
Answered By: The fourth bird
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.