| keller789 2006-11-06, 6:18 am |
| I have a text file of book information with titles, authors and the first chapter of each book displayed in the following way:
.T
title of book
.A
author of book
.C
once upon a time...
I want to be able to search through the file until I find a particular field (say .T) and then read off the following line. The only way I could think to do this was to convert the file into a single line by replacing the returns with spaces, and then to use a regular expression to pick out each piece of information.
The code looks something like -
while ($text = <INFILE> ) {
$text =~ s/\n/\t/g; #replace newline with space
if ($text =~ /(\.T)(.+)(\.A)/){ # find .T followed by any string followed by .A
$title = $2; # assign the middle string to $title
}
}
But $title is not returning anything, so I'm assuming that there is some problem with my RegEx.
If anybody could tell me what is wrong with my expression, or preferably suggest an easier way of going about the problem, I'd be very grateful.
Thanks, Sarah |