Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 正则表达式在数学表达式中添加空格(cab++=1+2+3->cab++=1+2+3)_Regex_Perl - Fatal编程技术网

Regex 正则表达式在数学表达式中添加空格(cab++=1+2+3->cab++=1+2+3)

Regex 正则表达式在数学表达式中添加空格(cab++=1+2+3->cab++=1+2+3),regex,perl,Regex,Perl,我有一个几乎可以工作的正则表达式: s/(?<!\+|-| )(\+|-)(?!=|\+|-| )/ $1 /g; 输出应为: cab += 1 + 2 + 3 + deb++ - 5 + -5; 我想处理所有的C/C++特殊情况,比如负数a=-C->a=-C,递增前后的变量a++=3->a++=3 这里有使用regexp的好方法吗?我为您写了一个sub,因为这些正则表达式一直在增长 sub format{ my $text = shift; #su

我有一个几乎可以工作的正则表达式:

 s/(?<!\+|-| )(\+|-)(?!=|\+|-| )/ $1 /g;
输出应为:

 cab += 1 + 2 + 3 + deb++ - 5 + -5;
我想处理所有的C/C++特殊情况,比如负数a=-C->a=-C,递增前后的变量a++=3->a++=3


这里有使用regexp的好方法吗?

我为您写了一个sub,因为这些正则表达式一直在增长

sub format{
        my $text = shift;
        #substitute '+'
        $text =~ s/(?<!\+)\+(?!\+|=)/ \+ /g;
        #substitute '-'        
        $text =~ s/(?<!-)-(?!-|=|>)/ - /g;
        #substitute '= , +=, -= (...)'
        $text =~ s/([\+,-,\*,\/]?=)/ $1 /g;

        #get rid of additional spaces:
        $text =~ s/  / /g;
        return $text;
}

是的,但在这种情况下,我不能处理像ted+++2+3+-I->ted+++2+3+-iIndeed这样的特殊情况。你上面的例子有点让人困惑。“ted-8”如何变成“ted++-8”?与“foo+bar”=>“foo+-bar”相同?一般来说,这是一件非常困难的事情。例如,关于C + + + + +的含义,C++ +++ +a+b+++a?@鲍罗丁i慢慢地认识到我的问题的复杂性:@硬币,Perl,解析Perl C++ C +++++A作为C+++++A+B+++A
sub format{
        my $text = shift;
        #substitute '+'
        $text =~ s/(?<!\+)\+(?!\+|=)/ \+ /g;
        #substitute '-'        
        $text =~ s/(?<!-)-(?!-|=|>)/ - /g;
        #substitute '= , +=, -= (...)'
        $text =~ s/([\+,-,\*,\/]?=)/ $1 /g;

        #get rid of additional spaces:
        $text =~ s/  / /g;
        return $text;
}
converting: foo+--bar++-3 += 3-x--+bar = ++c-*const->char++ +2
to:         foo + --bar++ - 3 += 3 - x-- + bar = ++c - *const->char++ + 2

converting: ++x->(a+b+--c) *= c++-++b/=9;
to:         ++x->(a + b + --c) *= c++ - ++b /= 9;

converting: b+c+a+d-=++*char->(--c+d);
to:         b + c + a + d- = ++*char->(--c + d);