Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: get the rest of the text

by Punitha (Priest)
on Feb 27, 2008 at 09:59 UTC ( [id://670568]=note: print w/replies, xml ) Need Help??


in reply to get the rest of the text

Hi steph_bow,

You can get the previous text from the matching string by using '$`' and the following text by using '$'' like,

#!/usr/bin/perl use strict; use warnings; my $element = "coucou"; open (INFILE, "<EXEMPLE.txt"); while (my $line = <INFILE>){ if ($line =~ /$element/){ my $pre=$`;###to get the previous text from the matching t +ext my $post=$';###to get the following text from the matching + text print "PREVIOUS:$pre\nFOLLOWING:$post\n"; } } close INFILE;

Punitha

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://670568]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found