匹配单词的第一个字母 [英] Matching first letter of word

查看:83
本文介绍了匹配单词的第一个字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个字符串中单词的第一个字母与具有相似字母的另一个字符串匹配.在此示例中,字母H:

I want to match the first letter of a word in one string to another with the similar letter. In this example the letter H:

25HB matches to HC

我正在使用如下所示的匹配运算符:

I am using the match operator shown below:

my ($match) = ( $value =~ m/^d(\w)/ );

不匹配数字,而是第一个匹配的单词字符.我该如何纠正?

to not match the digit, but the first matching word character. How could I correct this?

推荐答案

该正则表达式不会执行您认为的操作:

That regex doesn't do what you think it does:

m/^d(\w)/ 

匹配行首"-字母d然后是单个word character.

Matches 'start of line' - letter d then a single word character.

您可能想要:

m/^\d+(\w)/ 

然后从行首开始匹配一个或多个数字,然后抓取其后的第一个word character.

Which will then match one or more digits from the start of line, and grab the first word character after that.

例如:

my $string = '25HC'; 

my ( $match ) =( $string =~ m/^\d+(\w)/ );
print $match,"\n";

打印H

这篇关于匹配单词的第一个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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