N
Niall Macpherson
I don't use regexp / substitution handling very often and although I
think I have a basic grasp I am having problems with understanding how
to make multiple substitutions of different characters within a
string. I understand the use of appending a 'g' to the command for
multiple substitutions of the same pattern , but the following code
looks as if it could be improved.
I am trying to find the first occurence of anything between a '[' and
a ']'
and return that string
i.e the following code should print 'STRING'. It appears to work but
seems a bit long winded. Is there a better way of doing it ?
use strict;
use warnings;
use diagnostics;
sub GetString
{
my ($teststring) = @_;
if ($teststring =~ /\[.*\]/)
{
my $match = $&;
$match =~ s/\[//;
$match =~ s/\]//;
return($match);
}
else
{
return("");
}
}
my $input = " foo [STRING] bar ";
my $output = GetString($input);
print "Result = '$output'";
Thanks
think I have a basic grasp I am having problems with understanding how
to make multiple substitutions of different characters within a
string. I understand the use of appending a 'g' to the command for
multiple substitutions of the same pattern , but the following code
looks as if it could be improved.
I am trying to find the first occurence of anything between a '[' and
a ']'
and return that string
i.e the following code should print 'STRING'. It appears to work but
seems a bit long winded. Is there a better way of doing it ?
use strict;
use warnings;
use diagnostics;
sub GetString
{
my ($teststring) = @_;
if ($teststring =~ /\[.*\]/)
{
my $match = $&;
$match =~ s/\[//;
$match =~ s/\]//;
return($match);
}
else
{
return("");
}
}
my $input = " foo [STRING] bar ";
my $output = GetString($input);
print "Result = '$output'";
Thanks