在Perl中如何获取字符串中最后一个数字的索引?

huangapple go评论49阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年2月16日 19:09:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定