Perl 搜索并替换最后出现的字符 [英] Perl search and replace the last character occurrence

查看:46
本文介绍了Perl 搜索并替换最后出现的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我认为很容易解决的问题,但我无法找到答案.

I have what I thought would be an easy problem to solve but I am not able to find the answer to this.

如何查找并替换字符串中最后一次出现的字符?

How can I find and replace the last occurrence of a character in a string?

我有一个字符串:GE1/0/1,我希望它是:GE1/0:1 <- 这可以是可变长度,所以请不要使用子字符串.

I have a string: GE1/0/1 and I would like it to be: GE1/0:1 <- This can be variable length so no substrings please.

说明:我想用 : 替换最后一个/,无论它之前还是之后.

Clarification: I am looking to replace the last / with a : no matter what comes before or after it.

推荐答案

use strict;
use warnings;
my $a = 'GE1/0/1';
(my $b = $a) =~ s{(.*)/}{$1:}xms;
print "$b\n";

我使用了 .*

这篇关于Perl 搜索并替换最后出现的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆