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.

 



Raju_P
Novice

Dec 7, 2012, 11:15 PM

Post #1 of 4 (968 views)
Regular expression to grep the contetnts of a file. Can't Post

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)


Laurent_R
Veteran / Moderator

Dec 8, 2012, 1:26 AM

Post #2 of 4 (961 views)
Re: [Raju_P] Regular expression to grep the contetnts of a file. [In reply to] Can't Post

Something like this should do it:


Code
my $id = $1 if /^$index\.\s+\w+\s+Name,\s(ID\d+)$/;


You might also consider reading the file first and storing the values in an array, in which $ID[1] would be "ID123", etc. You would then have direct access to the IDs.


Raju_P
Novice

Dec 8, 2012, 2:14 AM

Post #3 of 4 (960 views)
Re: [Laurent_R] Regular expression to grep the contetnts of a file. [In reply to] Can't Post

Laurent_R, Thanks for you reply. The earlier text file was just as example. The actal text file looks like somewhat in the below.


If I enter option 1.

print $Variable

Result:
John Murphy Philip, wer23v

Contents of mytxt.txt.
1. John Murphy Philip, wer23v
2. Michael Tan, qw5fc3
3. Nancy, 2v8jws


Laurent_R
Veteran / Moderator

Dec 8, 2012, 6:00 AM

Post #4 of 4 (947 views)
Re: [Raju_P] Regular expression to grep the contetnts of a file. [In reply to] Can't Post

For obtaning help on regexes, you should really provide examples on actual data, not just pseudo-data actually looking totally different. I just can't guess your data if you give me something else.


Code
my $id = $1 if /^$index\.[^,]+,\s+(\w{6})$/;


should match your new data. My assumptions about the data format (rules that I derive from your examples):
- the line has one and only one comma, which separates the name from the ID
- the ID code is made up of 6 alphanumerical characters and sits at the end of the line.

If these assumptions are wrong, then we need to refine further the regex.

 
 


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

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