如何提取子串直到第一个数字? [英] How can I extract a substring up to the first digit?
本文介绍了如何提取子串直到第一个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何找到第一个子串直到找到第一个数字?
How can I find the first substring until I find the first digit?
示例:
my $string = 'AAAA_BBBB_12_13_14' ;
预期结果:'AAAA_BBBB_'
Result expected: 'AAAA_BBBB_'
推荐答案
从标签来看你要使用正则表达式.所以让我们建立它.
Judging from the tags you want to use a regular expression. So let's build this up.
- 我们希望从字符串的开头进行匹配,因此我们使用 a
锚定^
元字符 开头 - 我们想要匹配除数字以外的任何内容,因此我们查看字符类 并找出这是
\D
- 我们需要 1 个或多个,因此我们使用
+
量词 表示模式前一部分的 1 个或多个.
- We want to match from the beginning of the string so we anchor with a
^
metacharacter at the beginning - We want to match anything but digits so we look at the character classes and find out this is
\D
- We want 1 or more of these so we use the
+
quantifier which means 1 or more of the previous part of the pattern.
这给了我们以下正则表达式:
This gives us the following regular expression:
^\D+
我们可以像这样在代码中使用:
Which we can use in code like so:
my $string = 'AAAA_BBBB_12_13_14';
$string =~ /^\D+/;
my $result = $&;
这篇关于如何提取子串直到第一个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文