Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: get the rest of the text

by McDarren (Abbot)
on Feb 27, 2008 at 12:30 UTC ( #670610=note: print w/replies, xml ) Need Help??

in reply to get the rest of the text

As has been pointed out, you can use $'.
Although, this is best avoided if possible. And in this case it is certainly possible to avoid it by making use of capturing parentheses in your pattern match.

Here is a small snippet of code that demonstrates how this might be done:

#!/usr/bin/perl use strict; use warnings; my $wanted = 'coucou'; while (my $line = <DATA>) { chomp($line); my $rest = ''; if ($line =~ m/$wanted(.*)$/) { $rest = $1; print "$wanted:$rest\n"; } else { print "$wanted not found in $line\n"; } } __DATA__ abc coucou def not in this line in this line, but nothing following coucou coucou once, coucou twice, coucou three times - what should be done wi +th this line?

One thing you didn't specify in your question is what should happen if the string is found more than once in any line. The example above assumes that you would want everything after the first match.

If you wanted instead, everything after the last match, then you could achieve this by inserting a "greedy" dot-star at the beginning of the pattern match, like so:



Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2020-08-15 08:49 GMT
Find Nodes?
    Voting Booth?
    Which rocket would you take to Mars?

    Results (78 votes). Check out past polls.