O
Olaf
Which is the simplest way to know if a $var start and finish with the
same character?
same character?
Olaf said:Which is the simplest way to know if a $var start and finish with the
same character?
John said:$var =~ /\A(.).*\1\z/s;
Olaf said:$var=<STDIN>; chomp($var); if (($var =~ /^./) = ($var =~ /.$/ )) {}
John W. Krahn:
[head eq tail]
$var =~ /\A(.).*\1\z/s;
Ok, but I don't understand. I need to learn more about regular exp.
I suppose that the answer would be possible, comparing first and last
with ^ and $.
Olaf said:$var=<STDIN>; chomp($var); if (($var =~ /^./) = ($var =~ /.$/ )) {}
Dr.Ruud said:Olaf schreef:John W. Krahn:
Ok, but I don't understand. I need to learn more about regular exp.[head eq tail]
$var =~ /\A(.).*\1\z/s;
I suppose that the answer would be possible, comparing first and last
with ^ and $.
Read perlretut and perlre.
Hint-1: If your $var can contain newlines, you need to use "\A" in stead
of "^".
Dr.Ruud:Olaf:John W. Krahn:
[head eq tail]
$var =~ /\A(.).*\1\z/s;
Ok, but I don't understand. I need to learn more about regular exp.
I suppose that the answer would be possible, comparing first and
last with ^ and $.
Read perlretut and perlre.
Hint-1: If your $var can contain newlines, you need to use "\A" in
stead of "^".
Without the /m modifier \A and ^ are exactly the same.
Abigail said:John W. Krahn ([email protected]) wrote on MMMMDCCCXVI September
MCMXCIII in <URL:`` Olaf wrote:
`` > Which is the simplest way to know if a $var start and finish with the
`` > same character?
``
`` $var =~ /\A(.).*\1\z/s;
That would not match if $var equals "a". Which, IMO, starts and finishes
with the same character.
If you were going to do this with a regexp, I'd use:
$var =~ /^(?=(.)).*\1\z/s;
But I'd rather do:
length ($var) && substr ($var, 0, 1) eq substr ($var, -1, 1);
There's a time and a place for regular expressions. But the string
functions exist for a reason and sometimes they're better.
Are there any cases (within the scope of intended usage--fixed offsets
and search strings) that index, length, and substr are slower than
regular expressions? I don't know of any, and I'm curious.
Note that he wrote "better", not "faster". "Better" in code is usually a
mixture of several aspects and readability and maintainability are often
considered more important than performance.
Ted Zlatanov said:I think speed is the only aspect of code that everyone can agree on.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.