Doubt with Regex

Asked

Viewed 61 times

3

Good night.

I need a regular expression that can capture the numbers/expressions before and after a certain character, like this one ^.

For example, in the string below I need her to return me (5 * 77 + 4)^6 and 7^3:

5! + 8 - 5(ln(3)) + (5 * 77 + 4)^6 - 7^3

The expressions I’ve created so far are these:

  • /((\()?(.)+(\))?)+(\^)+((\()?(.)+(\))?)/g

  • /\(*\w+\)*(\^)+\(*\w+\)*/g

It is important that he check if there is only one ^.

In this example there is a functional application of the second case in a string, however I am not able to apply it in javascript.

  • Try: [ ] This expression uses the denied list.

  • @Denercarvalho, in which case it will not only capture the string excluding the ^?

  • Yes, now I understand what you want, my expression won’t work.

  • You want to isolate only the mathematical operation that uses power ^.

  • That’s right. At this link there is an example that this site works, but when I insert in javascript, it does not return me the correct value.

  • and because the expression cannot recognize the operations that are between relatives, both the right of the power ^ as for the left, only one operation alone 98 22.

  • It does not bring the correct values because groups are missing, see this example: https://regex101.com/r/uL2fQ0/5 But I still can’t get it to pick up other expressions. I’m trying to.

Show 2 more comments

1 answer

0


Well, what I wanted with the capture of these numbers/expressions was to simulate the action of the exponential function with the signal ^.

I got a way to do it, but it wasn’t just with regex.

First, I had to capture everything before the ^ so that syntactically this fit the exponential function. For example:

In the string below, I would capture the (2 - 9), ignoring white spaces, regardless of their quantity:

log(10) + (2 - 9) ^ 5

And then I’d have to do the same with what there was after the ^, what still in the above example would be the 5.

After that I should apply these captures in the javascript function Math, pow() that performs the exponentiation, according to what has already been said, would:

Math.pow((2 - 9), 5)

And finally, I should repeat this capture process>capture>concatenation until there is no more ^ in the midst of string. For so I could have an exponentiation within another exponentiation:

(2 ^ 3)^7

o Javascript

To capture what was before the ^, the code was that:

function captureBefore(string) {
  var result;
  var n = string.indexOf("^");
  var i = n - 1;
  while (!(string.charAt(i) != " ")) {
    --i;
  }
  if (!(/\d/.test(string.charAt(i)))) {
    var nOpen = 1;
    var nClose = 0;
    while (nOpen != nClose) {
      if (string.charAt(i - 1) == ")") {
        ++nOpen;
      } else if (string.charAt(i - 1) == "(") {
        ++nClose;
      }
      i -= 1;
    }
    if (/[a-z]/.test(string.charAt(i - 1))) {
      while ((/[a-zA-Z.]/.test(string.charAt(i - 1)))) {
        i -= 1;
      }
    }
    result = string.substring(i, n);
  } else {
    while (/\d/.test(string.charAt(i)) || /[-|+|.]/.test(string.charAt(i))) {
      i -= 1;
    }
    result = string.substring(i + 1, n);
  }
  return result;
}

And for what was after:

function captureAfter(string) {
  var result;
  var n = string.indexOf("^");
  var i = n + 1;
  while (string.charAt(i) == " " || /[a-zA-Z.]/.test(string.charAt(i))) {
    ++i;
  }
  if (/[-|+]/.test(string.charAt(i))) {
    ++i;
  }
  if (!(/(\d)/.test(string.charAt(i)))) {
    var nOpen = 1;
    var nClose = 0;
    while (nOpen != nClose) {
      if (string.charAt(i + 1) == "(") {
        ++nOpen;
      } else if (string.charAt(i + 1) == ")") {
        ++nClose;
      }
      i += 1;
    }
    result = (string.substring(n + 1, i + 1));
  } else {
    while (/\d/.test(string.charAt(i)) || /[-|+|.]/.test(string.charAt(i))) {
      i += 1;
    }
    result = (string.substring(n + 1, i));
  }
  return result;
}

And for it to repeat itself and function to reach all signals:

if (/\^/g.test(string)) {
  string = string.replace(/\s*\^\s*/g, "^")
  while (/\^/g.test(string)) {
    string = string.replace(captureBefore(string) + "^" + captureAfter(string), "Math.pow(" + captureBefore(string) + ", " + captureAfter(string) + ")");
  }
}

With this I was able to perform the function successfully. But if anyone has an idea that has the same result with less code, I’m still open to possibilities.

Functional example

Browser other questions tagged

You are not signed in. Login or sign up in order to post.