Repository: microsoft/PowerToys
File: src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/CalculateHelper.cs (C#)

Code before:

            // If the input ends with a binary operator then it is not a valid input to mages and the Interpret function would throw an exception. Because we expect here that the user has not finished typing we block those inputs.
            string trimmedInput = input.TrimEnd();
            if (trimmedInput.EndsWith('+') || trimmedInput.EndsWith('-') || trimmedInput.EndsWith('*') || trimmedInput.EndsWith('|') || trimmedInput.EndsWith('\\') || trimmedInput.EndsWith('^') || trimmedInput.EndsWith('=') || trimmedInput.EndsWith('&') || trimmedInput.EndsWith('/') || trimmedInput.EndsWith('%'))
            {
                return false;
            }

            return true;
        }

        public static string FixHumanMultiplicationExpressions(string input)
        {
            var output = CheckScientificNotation(input);
            output = CheckNumberOrConstantThenParenthesisExpr(output);
            output = CheckNumberOrConstantThenFunc(output);
            output = CheckParenthesisExprThenFunc(output);
            output = CheckParenthesisExprThenParenthesisExpr(output);
            output = CheckNumberThenConstant(output);
            output = CheckConstantThenConstant(output);
            return output;
        }

        private static string CheckScientificNotation(string input)
        {
            var p = @"(-?(\d+(\.\d*)?)|-?(\.\d+))[eE](-?\d+\.*\d*)";
            var r = "($1 * 10^($5))";
            return Regex.Replace(input, p, r);
        }

        /*
         * num (exp)
         * const (exp)
         */
        private static string CheckNumberOrConstantThenParenthesisExpr(string input)
        {
            var output = input;
            do
            {
                input = output;
                output = Regex.Replace(input, @"(\d+|pi|e)\s*(\()", m =>
                {
                    if (m.Index > 0 && char.IsLetter(input[m.Index - 1]))
                    {
                        return m.Value;
                    }

                    return $"{m.Groups[1].Value} * {m.Groups[2].Value}";
                });
            }
            while (output != input);

Review comment (suggestion):
```suggestion
            var p = @"(-?\d+(\.\d*)?)[eE](-?\d+(\.\d*)?)";
```
I've simplified the regex:
- Remove unnecessary capturing groups and consolidate mantissa section
- Fix the exponent section to be of the same form as the mantissa
  - Does not allow unlimited decimal points anymore

Code after:
            {
                return false;
            }

            // If the input ends with a binary operator then it is not a valid input to mages and the Interpret function would throw an exception. Because we expect here that the user has not finished typing we block those inputs.
            string trimmedInput = input.TrimEnd();
            if (trimmedInput.EndsWith('+') || trimmedInput.EndsWith('-') || trimmedInput.EndsWith('*') || trimmedInput.EndsWith('|') || trimmedInput.EndsWith('\\') || trimmedInput.EndsWith('^') || trimmedInput.EndsWith('=') || trimmedInput.EndsWith('&') || trimmedInput.EndsWith('/') || trimmedInput.EndsWith('%'))
            {
                return false;
            }

            return true;
        }

        public static string FixHumanMultiplicationExpressions(string input)
        {
            var output = CheckScientificNotation(input);
            output = CheckNumberOrConstantThenParenthesisExpr(output);
            output = CheckNumberOrConstantThenFunc(output);
            output = CheckParenthesisExprThenFunc(output);
            output = CheckParenthesisExprThenParenthesisExpr(output);
            output = CheckNumberThenConstant(output);
            output = CheckConstantThenConstant(output);
            return output;
        }

        private static string CheckScientificNotation(string input)
        {
            /**
             * NOTE: By the time the expression gets to us, it's already in English format.
             *
             * Regex explanation:
             * (-?(\d+({0}\d*)?)|-?({0}\d+)): Used to capture one of two types:
             * -?(\d+({0}\d*)?): Captures a decimal number starting with a number (e.g. "-1.23")
             * -?({0}\d+): Captures a decimal number without leading number (e.g. ".23")
             * E: Captures capital 'E'
             * (-?\d+): Captures an integer number (e.g. "-1" or "23")
             */
            var p = @"(-?(\d+(\.\d*)?)|-?(\.\d+))E(-?\d+)";
            return Regex.Replace(input, p, "($1 * 10^($5))");
        }

        /*
         * num (exp)
         * const (exp)
         */
        private static string CheckNumberOrConstantThenParenthesisExpr(string input)
        {
            var output = input;
            do
            {