简体   繁体   English

正则表达式挑战:改变负数的格式

[英]Regex challenge: changing formats of negative numbers

First of all, I think I have to apologize for not having that much knowledge of regular expressions (yet). 首先,我认为我必须为没有那么多关于正则表达式的知识而道歉(还)。 I have searched and searched, but haven't found a solution that matches my specific challenge here. 我搜索过并搜索过,但没有找到符合我特定挑战的解决方案。

Now here comes the question: 现在问题来了:

I am currently experimenting with the development of a parser (meaning to write it myself). 我目前正在尝试开发解析器(意思是自己编写)。 I would like to do this while taking regular expressions into account. 我想在考虑正则表达式时这样做。 Thus far, I have managed to do quite a lot, however, I am running into a small problem. 到目前为止,我已经设法做了很多,但是,我遇到了一个小问题。 The unary minus sign and the binary minus sign. 一元减号和二进制减号。

What I've learned on University about a decade ago, is that this should be based upon context. 我十年前在大学学到的是,这应该基于背景。 However, there is also a simple trick which I use when doing it by hand and that is writing every unary minus in to a different format: 但是,还有一个简单的技巧,我在手工操作时使用它,并将每个一元减去写入不同的格式:

-3 = (0 - 3) 

or 要么

5 * -3 = 5 * (0 - 3)

A bit more tricky: 有点棘手:

(5--5)-3 = (5 - (0 - 5)) - 3

Now, here is my question, is there a possibility to write a regular expression that translates a unary minus expression to a binary minus expression by adding parenthesis and a 0 , like in the examples provided above? 现在,这是我的问题,是否有可能编写一个正则表达式,通过添加括号和0来将一元减去表达式转换为二进制减去表达式,就像上面提供的示例一样?

Perhaps there might be another way too, but I am getting a little bit biased here... 也许可能还有另一种方式,但我在这里有点偏见......

Comment: First I started off with replacing all minus signs with the word MINUS like: 评论:首先,我开始用MINUS替换所有减号,如:

expressionBuffer = "-(1-2)-3";
expressionBuffer = Regex.Replace(expressionBuffer, "-", "MINUS");

This yields to a new expressionBuffer that looks like: 这产生了一个新的expressionBuffer,它看起来像:

MINUS ( 1 MINUS 2 ) MINUS 3

Now, I try to capture the binary '-' operators with applying the following regex: 现在,我尝试使用以下正则表达式捕获二进制' - '运算符:

expressionBuffer = Regex.Replace(
                     expressionBuffer, 
                     @"(?<number>((\d+(\.\d+)?)))\s+MINUS", 
                     "${number} -"
                   );

And this yields: 这会产生:

MINUS ( 1 - 2 ) MINUS 3

The first MINUS is clearly a unary operator (but the second is clearly not!) so I am now looking on a way to rewrite that one to the following (first) format: 第一个MINUS显然是一个一元的运算符(但第二个显然不是!)所以我现在正在寻找一种方法来重写那个以下(第一)格式:

( 0 - ( 1 - 2) ) MINUS 3

But here I am stuck to treat the first unary minus to a binary minus so that: 但在这里,我坚持将第一个一元减号视为二进制减号,以便:

( 0 - ( 1 - 2) ) - 3

Any ideas on how to use a regex for this? 关于如何使用正则表达式的任何想法?

Yes you can do that but you've to call the replacement operation as long as there are changes because the following regex will only replace a single unary operator: 是的,你可以这样做,但只要有变化就要调用替换操作,因为以下正则表达式只会替换单个一元运算符:

private static Regex _regex = new Regex(@"(?<=^|[-(+*/])-(?<value>\d+|\((?:[^\(\)]|(?<open>\()|(?<-open>\)))+?(?(open)(?!))\))", RegexOptions.Compiled);

private static string RemoveUnaryOperators(string input)
{
    var result = Regex.Replace(input ?? string.Empty, @"\s+", string.Empty);
    string tmp;
    do
    {
        tmp = result;
        result = _regex.Replace(result, @"(0-${value})");
    }
    while (result != tmp);

   return result;
}

DEMO DEMO

Output: 输出:

5*-3  ->  5*(0-3)
(5--5)-3  ->  (5-(0-5))-3
(5-------5)-3  ->  (5-(0-(0-(0-(0-(0-(0-5)))))))-3
-(1-2)-3  ->  (0-(1-2))-3

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

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