LinuxQuestions.org
Visit Jeremy's Blog.
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 09-07-2004, 02:12 AM   #1
mosh
LQ Newbie
 
Registered: Jun 2004
Posts: 10

Rep: Reputation: 0
Searching for 2 empty lines by RegEx in perl


Hi all,

I would like to search for 2 empty lines in a file by perl Reg Ex, how do I do that ?

Something like:

$line =~/\s\s/ ; # didn't work well.

Thanks in advance.
Mosh.
 
Old 09-07-2004, 04:35 AM   #2
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
The problem with that regex is \s applies to tabs, newlines, and spaces - so this would match two spaces, or a tab followed by a space, or other undesirables. Also, I believe, perl regexes default to single line mode, so what you need is this:

$line =~ /^\n\n/m;

assuming $line contains your lines of text. The m on the end forces the regex into "multiline" mode.
 
Old 09-07-2004, 05:53 AM   #3
mosh
LQ Newbie
 
Registered: Jun 2004
Posts: 10

Original Poster
Rep: Reputation: 0
The $line represents only one line...
Maybe that is the problem.

How can I make it to search for 2 blank lines in the whole file ?
 
Old 09-07-2004, 09:21 AM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
What some result output do you expect when searching for 2 blank lines in a text file ? The lines number ? Also they have to be consecutive or not ?
 
Old 09-07-2004, 08:16 PM   #5
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
You will need to keep a flag of some sort instead then, like this:

$flag = 0;
$line = 0;
while (<STDIN>) {
if ( /^$/ ) {
if ( $flag ) {
print "Double blank found at line: ".$line
} else {
$flag = 1;
}
} else {
$flag = 0;
}
$line++;
}
 
Old 09-07-2004, 10:19 PM   #6
b0ng
LQ Newbie
 
Registered: Aug 2004
Location: Location??? Where I am is top secret, if I tell you, I have to kill you.
Distribution: College, Slack
Posts: 24

Rep: Reputation: 15
Ugh... I am not sure that the person above me's will work. Simply because your <STDIN> is asking from input from the user. I am don't think this guy is trying to get input from the keyboard. I thought it was from a file.

Whoever has the question, just post the name and the directory that the file is in, and I will write you a quick little script.

Ugh... what in thee hell is "if ( /^$/ ) {" suppose to do? If I am reading that correct, if $_ is holding nothing, then continue.


Last edited by b0ng; 09-07-2004 at 10:21 PM.
 
Old 09-07-2004, 11:35 PM   #7
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
It's meant as an example, and assumes you pass the input through on STDIN - I don't know what his filename will be or how he reads it, but I've tested the script myself using a command like this:

cat file_with_blanks | /path/script.perl

In this case, the user would not be asked for input. Obviously, mosh could change it to use a different file handle, though I can't see why you'd want to write a generic script like this and then hardcode the filename. I'd think it more likely he'd want to use it on multiple files (either now or in the future). There is a semi-bug in that it will print the "double line" message twice for three blanks in a row, but it's easy to see how to get around that.


> Ugh... what in thee hell is "if ( /^$/ ) {" suppose to do? If I am reading that correct, if $_ is holding nothing, then continue.

Did you read the question? He's looking for empty lines. Geeze.

P.S: Sorry, the form ate my spaces - it does look quite ugly without some indenting.
 
Old 09-08-2004, 01:40 AM   #8
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
hey cromagnon, did you try the [ code ] [ /code ] (without space) markups and put your code inside
 
Old 09-08-2004, 02:51 AM   #9
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Cheers Cedrik That will come in handy...
 
  


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
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
perl regex question JustinHoMi Programming 5 03-20-2002 07:46 AM


All times are GMT -5. The time now is 03:22 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