ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
ada@barnabas:~/tmp> cat kram
this one \
this one not
this one again\
but not this line.
ada@barnabas:~/tmp> perl -ne 'if( /\\$/){ print }' kram
this one \
this one again\
ada@barnabas:~/tmp> perl -ne 'if( /\\\n$/){ print }' kram
this one \
this one again\
$filename = $ARGV[0];
open(INFILE, $filename);
while (<INFILE>) {
if (/\\$/) {
print $_;
print "That was a match\n";
}
}
close INFILE;
Here's the input file:
This line ends with a \
This is a new line
The text file was created by typing the first line, pressing enter, typing the second line, and closing the file without pressing enter again. This program prints:
This line ends with a \
That was a match
Since "That was a match" came on a new line, the last character of the first line is obviously a newline. But this regex should have only matched lines ending with '\' because of the $, so nothing should have matched. As a matter of fact, if I make the regular expression \\\n$ I get the same result. What is going on here? Thank you.
May I ask why it printed the same thing for \\\n$? If it doesn't look at the newline created when I pressed enter after the first line wouldn't it still not match because there is not a newline after the \?
$ matches at the end of the line (which by default means before the \n). If you explicitly put a \n in, then perl will match that too, as far as I can tell. Generally, you don't need to match \n explicitly.
/\\$/ matches a backslash at the end of the line (before the \n)
/\\\n$/ matches a backslash followed by a newline (which hits the absolute end of the line so is OK).
I'm not sure if all regex engines work like this, but perl does, apparently.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.