英文:
How to get the index of the last digit in a string in Perl?
问题
在Perl中,如何找到字符串中最后一个数字的索引?
示例:Hello123xyz
最后一个数字的索引是 7
。
英文:
In Perl, how do you find the index of the last digit in a string?
Example: Hello123xyz
index of last digit is 7
答案1
得分: 5
A RE to match the last digit in the string and the @-
variable to get the index of the start of the match:
#!/usr/bin/env perl
use warnings;
use strict;
use feature qw/say/;
sub last_digit_index($) {
if ($_[0] =~ /\d\D*\z/) {
return $-[0];
} else {
return -1;
}
}
say last_digit_index("Hello123xyz"); # 7
英文:
A RE to match the last digit in the string and the @-
variable to get the index of the start of the match:
#!/usr/bin/env perl
use warnings;
use strict;
use feature qw/say/;
sub last_digit_index($) {
if ($_[0] =~ /\d\D*\z/) {
return $-[0];
} else {
return -1;
}
}
say last_digit_index("Hello123xyz"); # 7
答案2
得分: 3
I'd probably use the pos
function here. Match with /g
and the Perl remembers where the match left off. The next global match on that string will start where the last match left off, so isolate this in a sub or block to avoid weird effects on subsequent matches on the same variable.
Since the position counts from 0, the next position will be one greater than the 1-based position of the final digit. You decide if you want to subtract 1 or not:
use v5.10;
say last_digit_pos('Hello123xyz');
sub last_digit_pos {
my( $string ) = @_;
$string =~ m/^.*\d/sg;
return pos($string); # 6
}
And, if the string doesn't match, pos
doesn't return a defined value.
英文:
I'd probably use the pos
function here. Match with /g
and the Perl remembers where the match left off. The next global match on that string will start where the last match left off, so isolate this in a sub or block to avoid weird effects on subsequent matches on the same variable.
Since the position counts from 0, the next position will be one greater than the 1-based position of the final digit. You decide if you want to subtract 1 or not:
use v5.10;
say last_digit_pos('Hello123xyz');
sub last_digit_pos {
my( $string ) = @_;
$string =~ m/^.*\d/sg;
return pos($string); # 6
}
And, if the string doesn't match, pos
doesn't return a defined value.
答案3
得分: 3
也可以利用 [List::MoreUtils::last_index](https://metacpan.org/pod/List::MoreUtils#last_index-BLOCK-LIST)
use List::MoreUtils qw(last_index);
my $last_digit_index = last_index { /[0-9]/ } split '', $string;
我觉得这很简单:将字符串使用 `split` 典型方式分割成字符列表,然后使用库来找到最后一个是数字的字符,通过一个简单的正则表达式。
请注意,这是“昂贵”的,因为它为每个字符创建一个标量,并多次运行正则表达式。所以如果效率很重要 - 如果这是在绝对庞大的字符串上执行的,或者在较小的字符串上多次执行很多次 - 那么最好寻求其他方法,或者至少在决定之前进行基准测试。
英文:
Can also leverage List::MoreUtils::last_index
use List::MoreUtils qw(last_index);
my $last_digit_index = last_index { /[0-9]/ } split '', $string;
I find this simple: break the string into a list of characters with a typical use of split
, and use a library to find the last one which is a digit, via a trivial regex.
Note that this is "expensive" as it creates a scalar for each character and runs regex multiple times. So if efficiency matters -- if this is done on an absolutely gigantic string, or many many many times on smaller strings -- then better seek other approaches, or at least benchmark before deciding.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论