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: Beginner:
Matching regular expressions

 



aiikahn
Novice

Mar 10, 2008, 9:57 AM

Post #1 of 4 (444 views)
Matching regular expressions Can't Post

I'm trying find out a way to print out the string for a given matching pattern but it keeps giving me the whole line. I'm not sure what I'm doing wrong...


Code
   

$_ = /[g]\w]/i;

I'd appreciate any assistance. Thank you. Still new with perl...


KevinR
Veteran


Mar 10, 2008, 11:13 AM

Post #2 of 4 (442 views)
Re: [aiikahn] Matching regular expressions [In reply to] Can't Post

Please post your perl code and show what the data you are searching is and what you are looking for and what you want to display.
-------------------------------------------------


aiikahn
Novice

Mar 10, 2008, 11:46 AM

Post #3 of 4 (441 views)
Re: [KevinR] Matching regular expressions [In reply to] Can't Post


Code
open (IN, "textfile.txt"); #textfile content "hello world\n this is a test"  

while (defined($test = <IN>) {
$test = m/[e]/i;
print $test;
}

close (IN);



Output should be:
hello
test

Instead, it gives me the whole content again. Thank you!


eWish
User

Mar 10, 2008, 4:48 PM

Post #4 of 4 (433 views)
Re: [aiikahn] Matching regular expressions [In reply to] Can't Post

The output you are getting is what you should be getting with the code you are using. You are checking to see the letter 'e' exists in the line not in each of the words in the line. $test holds the entire line.

If you want to split it into each of the words you can do something like this.

Code
open (my $FILE, '<', $file_to_open) || die "Can't open file: $!\n"; 
while (my $line = <$FILE>) {
for (grep /place pattern here/, split(/ /, $line)) {
print $_, "\n";
}
}
close ($FILE);



(This post was edited by eWish on Mar 10, 2008, 6:25 PM)

 
 


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

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