LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-16-2006, 04:19 PM   #1
ShaqDiesel
Member
 
Registered: Jul 2005
Posts: 144

Rep: Reputation: 15
Perl regex $


I want to match a line, $_ if it ends with \. There are several lines like this one after the other. Neither of these work, why?
/\\$/ nor /\\\n$/

BTW does subsititution just use the last filestream opened? Thanks.
 
Old 08-16-2006, 05:01 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

How are you trying to do this?

This seems to work, using /\\$/:
Code:
#!/usr/bin/perl

open(INFILE, "infile");
while (<INFILE>) {
  if (/\\$/) {
    print $_;
  }
}
close INFILE;
Hope this helps.
 
Old 08-16-2006, 05:22 PM   #3
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Actually both variants should work fine.
Code:
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\
 
Old 08-18-2006, 01:21 PM   #4
ShaqDiesel
Member
 
Registered: Jul 2005
Posts: 144

Original Poster
Rep: Reputation: 15
Here's some code:
#!/usr/bin/perl

$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.
 
Old 08-18-2006, 01:45 PM   #5
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
Quote:
But this regex should have only matched lines ending with '\' because of the $
$ matches BEFORE the \n, so it is working correctly.
 
Old 08-18-2006, 02:11 PM   #6
ShaqDiesel
Member
 
Registered: Jul 2005
Posts: 144

Original Poster
Rep: Reputation: 15
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 \?
 
Old 08-18-2006, 02:40 PM   #7
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
I didn't quite follow that.

$ 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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regex Perl help igotlongestname Programming 2 09-14-2005 07:51 PM
perl - regex - negation beebop Programming 9 06-19-2005 08:25 PM
simple perl and regex phlx Programming 6 12-03-2004 03:01 PM
Regex help needed in perl Dr Twox Programming 2 08-07-2004 05:58 AM
HELP ->PERL regex is kick'n my a... prisoner Programming 2 03-18-2004 09:49 PM


All times are GMT -5. The time now is 04:07 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration