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:
m/.*$wanted(.*)$/
Cheers,
Darren
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.