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: Regular expression to grep the contetnts of a file.: Edit Log



Raju_P
Novice

Dec 7, 2012, 11:15 PM


Views: 858
Regular expression to grep the contetnts of a file.

Hi,
Can some one assist me, how to write the regular expression to select the contents of a line from mytxt.txt file

Contents of mytxt.txt.
1. My Name, ID123
2. His Name, ID456
3. Her Name, ID789

By running the below program, by entering option 1, $Variable should be assigned with My Name, ID123
------------------------------------------------------------------------------------------------------


my (@list, $index, $variable, @user);
open(LIST, "mytxt.txt") or die "Cannot open mytxt.txt file. $!\n";
@list = <LIST>;
print @list;
close LIST;

print "Enter option: ";
#If option 1 is entered then $Variable should assign My Name, ID123.
#If option 2 is entered then $Variable should assign His Name, ID456.
chomp($index = <STDIN>);
while (@list)
{
chomp;
if (/(?<=$index.\s)(.+)/)---------> (I have used look behind option)
{
$Variable = $2;
}
}
print $Variable;

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


(This post was edited by Raju_P on Dec 8, 2012, 12:47 AM)


Edit Log:
Post edited by Raju_P (Novice) on Dec 8, 2012, 12:47 AM


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

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