bfdi533 has asked for the wisdom of the Perl Monks concerning the following question:
I have a file with a really long string in it (it is actually XML but for some reason it is stored in 1 line). What I need to do is to do a substring search of the file and print out the "word" that contains the substring. This "word" might be a url, a description, etc. For coding and extraction purposes, the "word" is delineated by whitespace. So I need to back up to the beginning of the "word" an print out to the end of the "word."
Here is the code I have already but as you can see it uses an absolute substring size and I need it to be dynamic:
while (<>) { my $istr = lc($_); my $offset = index($istr,"cesi"); print $offset."\n"; if ($offset > -1) { my $str = substr($istr, $offset-20, 100); print $str."\n"; } }
Thanks in advance for any input.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: substring extraction
by BrowserUk (Patriarch) on Jan 03, 2006 at 22:11 UTC | |
|
Re: substring extraction
by suaveant (Parson) on Jan 03, 2006 at 22:08 UTC | |
by bfdi533 (Friar) on Jan 05, 2006 at 15:54 UTC |