Regular Expression Grabbing

O

Owen

The Perl Cookbook 6.23 has this "bizarre" (to me) statement;

We have found these regular expressions useful or interesting.

Turning \ followed by n into a real newline
s/\\n/\n/g;

When would a \n not be a \n ? The program below sort of simulates that condition, but presumeably there is something else?

Owen

rcook at pcug org au



#----------------------------------------------------
#!/usr/bin/perl -w

use strict;
my $string ="Now is \n the time";
print "$string\n";

$string ='Now is \n the time';
print "$string\n";

(my $newstring = $string) =~ s/\\n/\n/g;
print "$newstring\n";

Now is
the time
Now is \n the time
Now is
the time
 
S

Shawn Corey

Owen said:
The Perl Cookbook 6.23 has this "bizarre" (to me) statement;

We have found these regular expressions useful or interesting.

Turning \ followed by n into a real newline
s/\\n/\n/g;

When would a \n not be a \n ?
# not a newline
print '\n';
# is a newline
print "\n";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top