Replace multiple newlines with single newlines during reading file
You could use a second regex to replace multiple new lines with a single new line and use strip to get rid of the last new line.
import os
import re
files=[]
pars=[]
for i in os.listdir('path_to_dir_with_files'):
files.append(i)
for f in files:
with open('path_to_dir_with_files/'+str(f), 'r') as a:
word = re.sub(r'someword=|\,.*|\#.*','', a.read())
word = re.sub(r'\n+', '\n', word).strip()
pars.append(word)
for k in pars:
print k
Replacing multiple empty lines with just one new line within string variable
Okay, I got it:
If x
is my string variable then
import re
x = re.sub(r'\n{2, 10}', '', x) # \n is new line, {2,10} is the range of occurences of the newline that I'm searching for.
This fixed the problem except for one exception. In certain cases it was removing all the newlines despite me wanting only one newline. This caused certain tickers to be bunched together like this:
'GALE''CEMP'
So I used another regular expression to fix this problem
import re
x = re.sub(r"''", "'\n'", x)
Everything look good now for the most part.
How to replace multiple newlines in a row with one newline using Ruby
This works for me:
#!/usr/bin/ruby
$s = "foo\n\n\nbar\nbaz\n\n\nquux";
puts $s
$s.gsub!(/[\n]+/, "\n");
puts $s
Replace Multiple New Lines in One New Line
Try using the following pattern:
/[\n\r]+/
as follows:
preg_replace( "/[\r\n]+/", "\n", $text );
What is the pythoninc way to replace multiple new line with single and single new line with one space?
Create a mapping dictionary:
dct = {'\n\n': '\n', '\n': ' '}
Using re.sub
(The order of this regex is important):
re.sub(r'(\n\n|\n)', lambda x: dct[x.group()], s)
Output:
'2 Our strategy drives sustainably higher profits and margins\nStrengthening our hubs is a critical foundation to maximize profitability\nDriving revenue improvements from all areas of business\nImproving efficiency and productivity \nGreater accountability and transparency '
A bit of explanation to how this works. Python's regular expression module does not support overlapping matches, so when it matches \n\n
, it will not also match \n
, which allows you to do both replacements in a single step.
Replace multiple new lines with a single newline
Try this one:
$str = "Hello\n\n\n\n\nWorld\n\n\nHow\nAre\n\nYou?";
$str = preg_replace("/\n+/", "\n", $str);
print($str);
Regex replace multiple new lines
The point is that your regex matches sequences of \r\n
(2 or more) and then 2 or more sequences of \r\r
. You need
[\r\n]+
Or [\r\n]{2,}
if you need to only match 2 or more occurrences of \r
or \n
.
If you need to exactly match 2 or more common line break types (\r\n
in Windows, \n
in Unix/Linux and \r
on Mac OS), use
(?:\r?\n|\r){2,}
How can I replace multiple empty lines with a single empty line in bash?
For BSD-derived systems (including GNU):
You just need cat
with the -s
option which causes it to remove repeated empty lines from its output:
cat -s
From man page: -s --squeeze-blank: suppress repeated empty output lines.
XSLT - Replace multiple new line with single new line
How about:
<xsl:template match="description">
<xsl:call-template name="normalize-returns">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template name="normalize-returns">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, '
')">
<!-- recursive call -->
<xsl:call-template name="normalize-returns">
<xsl:with-param name="text">
<xsl:value-of select="substring-before($text, '
')"/>
<xsl:text>
</xsl:text>
<xsl:value-of select="substring-after($text, '
')"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Related Topics
Http Referer Not Always Being Passed
How to Load the Requested Language File: Language/En/Form_Validation_Lang.Php
JavaScript or Jquery Doesn't Work in Dynamically Generated Content
Warning: MySQLi_Connect(): Unknown MySQL Server Host
PHP Code Can Insert Image to Excel File and Open It Correctly in Ms Excel
Get All Work Days in a Week for a Given Date
PHP Making a Download Counter Without Leaving the Current Page
How to Take a Screenshot of a Website with PHP and Gd
What's the Best and Easiest Way to Populate a Dropdown Based on Another Dropdown
Display Price on Add to Cart Button from the Functions.PHP File in Woocommerce
Patterns for PHP Multi Processes
How to Change the Name of an Element in Dom