Php 正则表达式取代货币模式
我想在这方面寻求帮助,我想检查字符串 匹配一个小数点后接一个字母(15米、15.3米、15吨或15.3吨)和 将其替换为零 例如: 15米->150万 1530万至1530万 15吨->15000吨 15.3T->15300 我试着用Php 正则表达式取代货币模式,php,regex,Php,Regex,我想在这方面寻求帮助,我想检查字符串 匹配一个小数点后接一个字母(15米、15.3米、15吨或15.3吨)和 将其替换为零 例如: 15米->150万 1530万至1530万 15吨->15000吨 15.3T->15300 我试着用str\u replace来做这件事,但做不好。希望 有人可以帮我 在Perl中: #!/usr/bin/perl use strict; use warnings; my %factors = ( B => 1_000_000_000,
str\u replace
来做这件事,但做不好。希望
有人可以帮我 在Perl中:
#!/usr/bin/perl
use strict;
use warnings;
my %factors = (
B => 1_000_000_000,
M => 1_000_000,
T => 1_000,
);
while ( <DATA> ) {
next unless m{
^
(?<number> -? [0-9]+ (?:\.[0-9]+)? )
(?<factor>B|M|T)?
$
}x;
my $number = $+{factor}
? sprintf( '%.0f', $+{number} * $factors{ $+{factor} } )
: $+{number}
;
print $number, "\n";
}
__DATA__
15M
15B
15T
15.3M
15.3B
15.3T
15
15.3
在Perl中:
#!/usr/bin/perl
use strict;
use warnings;
my %factors = (
B => 1_000_000_000,
M => 1_000_000,
T => 1_000,
);
while ( <DATA> ) {
next unless m{
^
(?<number> -? [0-9]+ (?:\.[0-9]+)? )
(?<factor>B|M|T)?
$
}x;
my $number = $+{factor}
? sprintf( '%.0f', $+{number} * $factors{ $+{factor} } )
: $+{number}
;
print $number, "\n";
}
__DATA__
15M
15B
15T
15.3M
15.3B
15.3T
15
15.3
“T”可能是“千”或“万亿”,你知道
$s = "15.7T";
$factors = Array('T' => 1000, 'M' => 1000000, 'B' => 1000000000.);
$matches = Array();
if (0 < preg_match('/([0-9]+(?:\.[0-9]*)?)([TMB])/', $s, $matches)) {
print("$s -> " . $matches[1] * $factors[$matches[2]]);
}
编辑:
锚定(使正面或背面的垃圾表示不匹配):
您可能希望允许空白,但是:
'/^\s*(...)\s*$/'
您也可以使用“\d”代替“[0-9]:
不区分大小写:
'/.../i'
...
print("$s -> " . $matches[1] * $factors[strtoupper($matches[2])]);
可选因素:
'/([0-9]+(?:\.[0-9]*)?)([TMB])?/'
$value = $matches[1];
$factor = isset($matches[2]) ? $factors[$matches[2]] : 1;
$output = $value * $factor;
使用printf控制输出格式:
print($value) -> 1.57E+10
printf("%f", $value); -> 15700000000.000000
printf("%.0f", $value); -> 15700000000
一个巧妙的把戏,把“?”(可选的)放在括号内,保证一个匹配字符串,它可能是空的。然后你可以使用空白作为数组关键字来获得默认值,而不必测试它是否匹配。
综上所述:$factors = Array('' => 1, 'T' => 1e3, 'M' => 1e6, 'B' => 1e9);
if (0 < preg_match('/^\s*(\d+(?:\.\d*)?)([TMB]?)\s*$/i', $s, $matches)) {
$value = $matches[1];
$factor = $factors[$matches[2]];
printf("'%s' -> %.0f", $s, $value * $factor);
}
$factors=数组(''=>1,'T'=>1e3,'M'=>1e6,'B'=>1e9);
如果(0%.0f“,$s,$value*$factor);
}
“T”可能是“千”或“万亿”,你知道
$s = "15.7T";
$factors = Array('T' => 1000, 'M' => 1000000, 'B' => 1000000000.);
$matches = Array();
if (0 < preg_match('/([0-9]+(?:\.[0-9]*)?)([TMB])/', $s, $matches)) {
print("$s -> " . $matches[1] * $factors[$matches[2]]);
}
编辑:
锚定(使正面或背面的垃圾表示不匹配):
您可能希望允许空白,但是:
'/^\s*(...)\s*$/'
您也可以使用“\d”代替“[0-9]:
不区分大小写:
'/.../i'
...
print("$s -> " . $matches[1] * $factors[strtoupper($matches[2])]);
可选因素:
'/([0-9]+(?:\.[0-9]*)?)([TMB])?/'
$value = $matches[1];
$factor = isset($matches[2]) ? $factors[$matches[2]] : 1;
$output = $value * $factor;
使用printf控制输出格式:
print($value) -> 1.57E+10
printf("%f", $value); -> 15700000000.000000
printf("%.0f", $value); -> 15700000000
一个聪明的技巧,把“?”(可选)放在parens中,你保证有一个匹配字符串,它可能是空的。然后,可以使用空白作为数组密钥来获取默认值,而不必测试它是否匹配。
综上所述:
$factors = Array('' => 1, 'T' => 1e3, 'M' => 1e6, 'B' => 1e9);
if (0 < preg_match('/^\s*(\d+(?:\.\d*)?)([TMB]?)\s*$/i', $s, $matches)) {
$value = $matches[1];
$factor = $factors[$matches[2]];
printf("'%s' -> %.0f", $s, $value * $factor);
}
$factors=数组(''=>1,'T'=>1e3,'M'=>1e6,'B'=>1e9);
如果(0%.0f“,$s,$value*$factor);
}
对找到的所有价格执行转换的替代版本:
$multiply = array('' => 1, 'T' => 1000, 'M' => 1000000);
function convert($matches) {
global $multiply;
return $matches[1] * $multiply[$matches[3]];
}
$text = '15M 15.3M 15.3T 15';
print(preg_replace_callback('/(\d+(\.\d+)?)(\w?)/', 'convert', $text));
对找到的所有价格执行转换的替代版本:
$multiply = array('' => 1, 'T' => 1000, 'M' => 1000000);
function convert($matches) {
global $multiply;
return $matches[1] * $multiply[$matches[3]];
}
$text = '15M 15.3M 15.3T 15';
print(preg_replace_callback('/(\d+(\.\d+)?)(\w?)/', 'convert', $text));
我不知道如何在一个正则表达式中实现它,下面是我在Ruby中的尝试
#!/usr/bin/ruby
def trans s
h={"M"=>10**6 ,"T"=>10**3}
a = s.split(/[MT]/) + [s.split("").last]
(a[0].to_f * h[a[1]]).to_i
end
puts trans "15M"
puts trans "15.3M"
puts trans "15T"
puts trans "15.3T"
我不知道如何在一个正则表达式中实现它,下面是我在Ruby中的尝试
#!/usr/bin/ruby
def trans s
h={"M"=>10**6 ,"T"=>10**3}
a = s.split(/[MT]/) + [s.split("").last]
(a[0].to_f * h[a[1]]).to_i
end
puts trans "15M"
puts trans "15.3M"
puts trans "15T"
puts trans "15.3T"
哦,我在发布之前没有看到这些评论。哦,我在发布之前没有看到这些评论。你能用php翻译吗?我不懂perl,谢谢你的回答。对不起,我没有意识到这是一个PHP问题。Codebender的解决方案在某种程度上与我的相当。太糟糕了,PHP不支持命名捕获,这很酷。它们也是Perl最近添加的。你能用PHP翻译它吗?我不懂perl,谢谢你的回答。对不起,我没有意识到这是一个PHP问题。Codebender的解决方案在某种程度上与我的相当。糟糕的是,PHP不支持命名捕获,这很酷。它们也是Perl的最新添加。+1。但是,您可能希望锚定模式,并使因子可选,以防有没有数据的数字。是的,我试图保持简单,因为没有关于输入可能在什么上下文中的线索。嘿,我刚刚检查了,如果以百万为单位,它将以指数形式返回数字(1.4M->1.4E+6),如何将其转换为零?嗯,我可以得到“15.7B->157000000”,但我在PHP.ini文件中设置了“precision=14”。我将更新答案。Stephan202,代码的哪一部分将被更新?是默认值为+1。但是,您可能希望锚定模式,并使因子可选,以防有没有数据的数字。是的,我试图保持简单,因为没有关于输入可能在什么上下文中的线索。嘿,我刚刚检查了,如果以百万为单位,它将以指数形式返回数字(1.4M->1.4E+6),如何将其转换为零?嗯,我可以得到“15.7B->157000000”,但我在PHP.ini文件中设置了“precision=14”。我将更新答案。Stephan202,代码的哪一部分将被更新?是默认值是AppAlignice,我甚至不知道该函数存在。如果您可以将factor映射作为一个参数,并为回调传递一个curry版本,那就更酷了,但是在PHP中使用curry是一件痛苦的事情。巧妙地使用空白字符串作为一个关键。很好,我甚至不知道函数存在。如果您可以将factor映射作为一个参数,并为回调传递一个curry版本,那就更酷了,但是在PHP中使用curry是一件痛苦的事情。巧妙地使用空白字符串作为一个关键。