简体   繁体   English

正则表达式匹配特定字符集之间的整数

[英]Regex to match integers between specific set of characters

I need to replace all integers after and before these specific characters: ( ) * - / % + space but nothing than these.我需要在这些特定字符之前和之后替换所有整数:( ( ) * - / % + space但仅此而已。

So (34 + should match but a34 + or k3- should not.所以(34 +应该匹配但a34 +k3-不应该。

I have this so far, '/(?:-| |\\(|\\)|\\+|\\*|\\/|%)(\\d+)(?:-| |\\(|\\)|\\+|\\*|\\/|%)/' but this is not working like what I want it to.到目前为止,我有这个, '/(?:-| |\\(|\\)|\\+|\\*|\\/|%)(\\d+)(?:-| |\\(|\\)|\\+|\\*|\\/|%)/'但这不像我想要的那样工作。

$pattern = '/[- ()+*\/%](\d+)[- ()+*\/%]/';
$replacement = "xyz($1)";
$insideFunc = preg_replace($pattern, $replacement, $insideFunc);

$insideFunc = "float y = 45*(3-max(3-float(ceil(3)), 3-float(floor(3))))*2.302585092994046"

output -> float y =xyz(45)xyz(3)maxxyz(3)float(ceilxyz(3)),xyz(3)float(floorxyz(3))))*2.302585092994046

I want it to be, float y = xyz(45)*(xyz(3)-max(xyz(3)-float(ceil(xyz(3))), xyz(3)-float(floor(xyz(3)))))*2.302585092994046我希望它是, float y = xyz(45)*(xyz(3)-max(xyz(3)-float(ceil(xyz(3))), xyz(3)-float(floor(xyz(3)))))*2.302585092994046

I changed it like that and it's working now.我像那样改变了它,现在它正在工作。

$pattern = '/([- ()+*\/%])(\d+)([- ()+*\/%])/';
$replacement = "$1float($2)$3";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM