Replacing multiple occurences of newline chars with a single newline char 
Author Message
 Replacing multiple occurences of newline chars with a single newline char

Hi,

i would like to make use of the power of Regular Expression
matching/parsing in Perl.

I have a string which might contain multiple occurences of
newlines ("\n") at different places and i want to replace all
those newline occurences with only 1 single newline char. Hence
$text should become $text_final:

$text = "a b   c d \n \n \n fgh \n i j kl \n \n";
$text_final => "a b   c d \n fgh \n i j kl \n";

Ive implement a loop-counter mechanism whereby i count the no.
of "\n"s and if $count > 1, then just substitute with 1
single "\n" char. However, i would like to use the Reg Expr
feature.

Any advice? Thanks!

GC/SG.

* Sent from RemarQ http://www.*-*-*.com/ The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!



Wed, 18 Jun 1902 08:00:00 GMT  
 Replacing multiple occurences of newline chars with a single newline char

  g> I have a string which might contain multiple occurences of
  g> newlines ("\n") at different places and i want to replace all
  g> those newline occurences with only 1 single newline char. Hence
  g> $text should become $text_final:

look for the tr op in perlop and the /s modifier. does exactly what you
want.

uri

--

SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page  -----------  http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net  ----------  http://www.northernlight.com



Wed, 18 Jun 1902 08:00:00 GMT  
 Replacing multiple occurences of newline chars with a single newline char


Quote:
> i would like to make use of the power of Regular Expression
> matching/parsing in Perl.

ITYM the 'awesome' power ...  :-)

Quote:
> I have a string which might contain multiple occurences of
> newlines ("\n") at different places and i want to replace all
> those newline occurences with only 1 single newline char. Hence
> $text should become $text_final:

> $text = "a b   c d \n \n \n fgh \n i j kl \n \n";
> $text_final => "a b   c d \n fgh \n i j kl \n";

> Ive implement a loop-counter mechanism whereby i count the no.
> of "\n"s and if $count > 1, then just substitute with 1
> single "\n" char. However, i would like to use the Reg Expr
> feature.

> Any advice? Thanks!

The problem is slightly underspecified.  The following replaces a
sequence of any number of spaces followed by a newline, repeated twice
or more, with a single space and a newline, as in your example.  Flavor
to taste.

#!/usr/bin/perl -w
use strict;

my $text = "a b   c d \n \n \n fgh \n i j kl \n \n";
my $text_final = "a b   c d \n fgh \n i j kl \n";

$text =~ s/(?: *\n){2,}/ \n/g;

print $text unless $text eq $text_final;

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Wed, 18 Jun 1902 08:00:00 GMT  
 Replacing multiple occurences of newline chars with a single newline char


Quote:

>   g> I have a string which might contain multiple occurences of
>   g> newlines ("\n") at different places and i want to replace all
>   g> those newline occurences with only 1 single newline char. Hence
>   g> $text should become $text_final:

> look for the tr op in perlop and the /s modifier. does exactly what you
> want.

In the sample input and output, each "\n" was preceded by a space.  I
chose to deal with that using a regex substitution.  If the spaces were
conceptual, not real, then your tr solution is best.

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Wed, 18 Jun 1902 08:00:00 GMT  
 Replacing multiple occurences of newline chars with a single newline char
: i would like to make use of the power of Regular Expression
: matching/parsing in Perl.

You can do it that way, but sometimes that's not the best tool.

: I have a string which might contain multiple occurences of
: newlines ("\n") at different places and i want to replace all
: those newline occurences with only 1 single newline char. Hence
: $text should become $text_final:
:
: $text = "a b   c d \n \n \n fgh \n i j kl \n \n";
: $text_final => "a b   c d \n fgh \n i j kl \n";

Are those spaces between the newlines real, or put in for clarity?

If they're real, which seems weird:

  $text =~ s/\n(?:\s*?\n)+/\n/g;

If they're not:

  $text =~ tr/\n//s;

--

 --*--  http://www.cinenet.net/users/cberry/home.html
   |   "The road of Excess leads to the Palace
      of Wisdom" - William Blake



Wed, 18 Jun 1902 08:00:00 GMT  
 Replacing multiple occurences of newline chars with a single newline char




  >>
  g> I have a string which might contain multiple occurences of
  g> newlines ("\n") at different places and i want to replace all
  g> those newline occurences with only 1 single newline char. Hence
  g> $text should become $text_final:
  >>
  >> look for the tr op in perlop and the /s modifier. does exactly what you
  >> want.

  LR> In the sample input and output, each "\n" was preceded by a space.  I
  LR> chose to deal with that using a regex substitution.  If the spaces were
  LR> conceptual, not real, then your tr solution is best.

as you said, the problem wasn't defined well. my take was the spaces wee
for clarity and not real as that would be a wierd set of data. also you
can squeeze multiple spaces and newlines to a single newline as well
as i am sure you know. maybe the OP could respond and clarify?

uri

--

SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page  -----------  http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net  ----------  http://www.northernlight.com



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Removing newline chars

2. Replacing special chars with special chars

3. replacing multiple newlines?

4. Replace single chars with many?

5. Substituting n occurences of a char with one

6. Help search/replace a string with newlines

7. Multi-line substitution (adding newlines and global replace)

8. How to replace the newline in perl?

9. Perl (filter newlines & replace with space)

10. Replacing Macintosh newline (\x0d) with \n ?

11. @chars the same as ($#chars) ?

12. Q: using a c-lib with char** and even char***

 

 
Powered by phpBB® Forum Software