[英]perl regular expression digit on string
我試圖從字符串中獲取數字。 數字可以是純數字,例如12334
,也可以用下划線12_345
我嘗試使用以下代碼,但無法從中獲取任何信息。
my $string = "this is a 141_153_923 number : $_123_456";
if ($string =~ /\b\d*(?:\d+\_?\d+)*\d*\b/) {
print "$&\n";
}
預期產量為141_153_923
我也嘗試過字符串141_153_923
,它仍然沒有返回任何東西,甚至與
$string =~ /\b\d\b/
在字符串141_153_923
我希望你的Perl
代碼中聲明了變量$_123_456
。 否則你會收到警告。
現在的正則表達式。 試試這個:
if ($string =~ /\b(\d+(?:_\d+)*)\b/) {
試試這個正則表達式: /((?:\\d+\\_?)+)/
。
...
my $string = "this is a 141_153_923 number : \$_123_456";
my $num;
if (($num) = $string =~ /((?:\d+\_?)+)/) {
print "first: $num\n";
}
$string = "this is a 141153923 number : \$_123_456";
if (($num) = $string =~ /((?:\d+\_?)+)/) {
print "second: $num\n";
}
...
輸出:
first: 141_153_923
second: 141153923
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.