CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
General Q about regex matches VI vs. Perl

 



diemaker
New User

Sep 28, 2003, 4:17 PM

Post #1 of 9 (5528 views)
General Q about regex matches VI vs. Perl Can't Post

I have a simple text file with \n at the end of each line.

Using VI to find \w\n, it highlights the last character and \n at the end of each line.

In my Perl code, @text = <FILE> and then my $stext = "@text";

Perl can find the \n, but not \w\n. If I put \s\n, then it finds every \n, but there is no \s before the \n, only word characters.

I output $stext to a file and again using VI, I can find \w\n.

A here-document (text in the code itself) works as I would expect it and my code finds all \n and \w\n correctly.

In this case and in general, why does Perl not find what VI clearly indicates is present?


(This post was edited by diemaker on Sep 28, 2003, 4:18 PM)


KevinR
Veteran


Sep 28, 2003, 6:35 PM

Post #2 of 9 (5523 views)
Re: [diemaker] General Q about regex matches VI vs. Perl [In reply to] Can't Post

it wil take a more experienced person than me to explain why this is, but if your line only contains the \n at the very end, and it should, you do not use the end of string pattern anchor ($) if that is what you are doing. Since there is only one \n what you need to do is search for /\w\n/ only. Since you did not post the code you are attempting to use I can only offer a generic example:



if ($stext =~ m/\w\n/) {

print "matched";

}
-------------------------------------------------


diemaker
New User

Sep 28, 2003, 9:00 PM

Post #3 of 9 (5518 views)
Re: [KevinR] General Q about regex matches VI vs. Perl [In reply to] Can't Post

Thanks for the reply KevinR. Let me clarify a little. Take this sample file for example:

abc
def
ghi

A search in VI for :/\w\n will find the last letter (c, f, and i) in each line and the \n at the end of each line.

But in Perl, a match for \n works, but I get no matches with a search for \w\n only a match when using \s\n.

sample code to count matches:

my $count=0;
while ($stext =~ /\w\n/g) {
$count++;
}

So why does VI match \w\n with no problem but Perl does not in the same file? The \w\n pattern is not hard to identify. What do people use to clearly see all visible and special characters in a file?


(This post was edited by diemaker on Sep 28, 2003, 9:06 PM)


KevinR
Veteran


Sep 28, 2003, 9:21 PM

Post #4 of 9 (5512 views)
Re: [diemaker] General Q about regex matches VI vs. Perl [In reply to] Can't Post

maybe your example is just not a good one, but what you posted will not work very well for iterating over a multiline file. Something along these lines should work;


Code
@text = ( 
"abc\n",
"def\n",
"ghi\n",
"jkl\n"
);


$count = 0;
for (@text) {
$count++ if (/\w\n/);
}

print $count;

-------------------------------------------------


diemaker
New User

Sep 28, 2003, 9:32 PM

Post #5 of 9 (5509 views)
Re: [KevinR] General Q about regex matches VI vs. Perl [In reply to] Can't Post

Yes. If I put the same text file into the code with:

$_ = <<EOF;
...text file inserted here....
EOF

Then editing the code in VI finds \w\n and doing a count on $_ also finds \w\n. It's when I read it in from a file and then try to find \w\n that I have the problem.


KevinR
Veteran


Sep 28, 2003, 9:41 PM

Post #6 of 9 (5508 views)
Re: [diemaker] General Q about regex matches VI vs. Perl [In reply to] Can't Post


In Reply To
Thanks for the reply KevinR. Let me clarify a little. Take this sample file for example:

abc
def
ghi

A search in VI for :/\w\n will find the last letter (c, f, and i) in each line and the \n at the end of each line.

But in Perl, a match for \n works, but I get no matches with a search for \w\n only a match when using \s\n.

sample code to count matches:

my $count=0;
while ($stext =~ /\w\n/g) {
$count++;
}

So why does VI match \w\n with no problem but Perl does not in the same file? The \w\n pattern is not hard to identify. What do people use to clearly see all visible and special characters in a file?



I don't know what VI is so I can't answer that. Your "while" loop might just run forever if the match returns true. Thats not good! It would be better as an "if" condition:

$count=0;
if ($stext =~ m/\w\n/) {
$count++;
}

how to view hidden characters in files? Good question, I have an editor that shows hidden characters, it shows a paragraph symbol for the \n character but I don't know how to post character entities on this message board. (Its a funny looking P). Shows -> for tabs and etc.
-------------------------------------------------


KevinR
Veteran


Sep 28, 2003, 9:51 PM

Post #7 of 9 (5506 views)
Re: [diemaker] General Q about regex matches VI vs. Perl [In reply to] Can't Post

this should work to do what you want for a text file:


Code
 open (FILE, "path/to/your/text_file");  
$count = 0;
while ( <FILE>) {
$count++ if (/\w\n/);
}
close (FILE);

print $count;

and with that I bid you good luck and goodnight Smile
-------------------------------------------------


(This post was edited by KevinR on Sep 28, 2003, 9:54 PM)


diemaker
New User

Sep 29, 2003, 9:31 PM

Post #8 of 9 (5501 views)
Re: [KevinR] General Q about regex matches VI vs. Perl [In reply to] Can't Post

Thanks for the suggestions KevinR. All of those are ways to setup the code to find and count matches, but Perl is not finding the regular expression I specify. My basic question is why does Perl not find \w\n when it seems to clearly be present in the text file - vi matches the string \w\n and Perl finds the correct number of \n, just not \w\n (it sees \s\n instead).


KevinR
Veteran


Sep 29, 2003, 11:05 PM

Post #9 of 9 (5498 views)
Re: [diemaker] General Q about regex matches VI vs. Perl [In reply to] Can't Post

My last post is not a "setup", it opens a text file, any text file, and looks for the combination of \w\n and if it is present it will increment the count, I thought that is what you wanted to do. Your way will not work. It might with VI, but not with Perl.

There is no trick in my code, it will rteturn true if \w\n is matched and false if its not matched. If you must put your file into an array that will also work:

open(FILE,"yourfile.txt");

@array = <FILE>;

close (FILE);

$count = 0;

for (@array) {

$count++ if (/\w\n/);

}

but my other code will be a little easier on the sytem resources if its a big file since its not loading the file into memory (@array) but reading directly from the file. But if you insist on doing your way:

$stext = "@text";

then trying that while loop you posted, it will not work. VI might beable to do it that way, but Perl will not.

Nuff said. Wink
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives