Reputation: 75127
I have a variable my $html
and it holds a string value. I want to cut it after the word
</SELECT>
How can I do that with Perl?
Upvotes: 0
Views: 11478
Reputation: 41
If you want to get the text which stands before the first </SELECT>
you could use a regex like this:
$html =~ /(.*?<\/SELECT>)/;
my $required_text = $1;
or in one step:
my ($required_text) = $html =~ /(.*?<\/SELECT>)/;
It uses the non-greedy modifier ?
which I think is what you need.
Or better the substr()
subroutine like the previous answer says.
Upvotes: 0
Reputation: 38745
Don't use a regexp (power saw) when a knife (look for fixed string) is enough:
my $html = 'use dom to work with </SELECT> html!';
my $cut = '</SELECT>';
printf "|%s|\n", substr( $html, index( $html, $cut ) + length( $cut ) );
==>
| html!|
Upvotes: 1
Reputation: 62037
use warnings;
use strict;
my $html = '<SELECT>foo</SELECT> bar';
$html =~ s{(</SELECT>).*}{$1};
print "$html\n";
__END__
<SELECT>foo</SELECT>
You should also consider using one of the many HTML parsers on CPAN.
Upvotes: 3
Reputation: 129373
$html =~ s#</SELECT>.*$#</SELECT>#;
You can probably write it without the </SELECT>
being in the replacement string, but IMHO it would be less readable.
Please note that s/// substitution can use other characters aside from /
as separators, and in this case I chose to use #
because your regular expression contains forward slash character that would otherwise have to be escaped making the regex less readable
Upvotes: 2