Order of evaluation of the alternatives in a regular expression is always taken into account?

Asked

Viewed 107 times

0

I was here developing a function that took a logical string of characters, let’s call "query", this query represents simple expressions separated by ;, expressions can contain any of these operations: ==, !+, >, <, <=, =>,~= that represent a validation between attribute and value, I need to take this string and separate it into an array of objects with each of these separate properties.

For example:

"temperature==40;engine!= fail;speed>90;speed<90;speed>=90;speed<=90;speed~=90;speed~=90"

viraria:

[ {attr: "temperature",op: "==",value:"40"}, {...} ] I solved this problem using . split passing the regular expression /(==|!=|>=|<=|~=|>|<)/, only I’m not sure if the regular expression will run in order, as I have >= and <= i put > and < in the end, so that it would not be captured without analyzing all the alternatives of the expression.

var exprA = /(==|!=|>=|<=|~=|>|<)/;
var exprB = /(==|!=|~=|>|<|>=|<=)/;
var q = "temperature==40;engine!=fail;speed>90;speed<90;speed>=90;speed<=90;speed~=90";
var opts = q.split(';');
console.info('Expr. A');
for (var i in opts) {
  var parts = (opts[i] || "").split(exprA);
  console.log(parts.join(' '));
}

console.info('Expr. B:');
for (var i in opts) {
  var parts = (opts[i] || "").split(exprB);
  console.log(parts.join(' '));
}

See in the example above, in "expression B" the result was returned first to > and not >=, which did not occur in expression A, the theory is confirmed but I’m not sure.

Is there any RFC documentation that confirms that regular expressions execute their options in order in all programming situations, uses and languages?

  • When you say order, you mean the order of the regex analysis? or if the return is in order of capture?

  • Exactly, if in the group I created in the refez(vide snippet) it will return the result in the order the conditions were written in regex, or if it depends

  • I see you edited the question, that’s right, all Regex works the same way (maybe have different operators for each thing, but the operation is the same), it will first analyze what is left and gradually checking whether each OR is satisfied, until the end of the expression, on the right.

  • I edited the question to clarify my doubt. And I changed the references in the snippet.

  • Check if this is what you wanted, I hope to have helped :D

1 answer

1


Is there any RFC documentation to confirm that expressions run your options in order in all situations, uses and programming languages?

Yes, you can find documentation about your problem here:

See in the example above, in "expression B" the result was returned first to > and not >=, which did not occur in expression A, the theory is confirmed but not sure.

What you tested with the expressions To and B, is correct and is reproduced in all Regex Mills, what changes in them are the their tokens, Operators and modifiers.
Then I can confirm (after all you just proved) that:
A regex with the OR operator (|) checks its possibilities from left to right and once satisfied, no longer checks that character, follows the analysis from the next

That’s why even if you have the possibility to be found both "<=" and "<" express the search in regex in this way: (<|<=)

In a text like this:1<=1

You will capture only <, because the regex will check whether the first condition has been satisfied and then the second in each character and IF the condition is completely satisfied, nor will it analyze the second possibility, will resume the analysis from the next position in the string.

So in case this little analysis

  • regex first checks the 1.
    meets the first condition? NAY
    meets the second condition? NAY
  • Then the <
    meets the first condition? YES / COMPLETELY? YES
    Then capture and follow the analysis.
  • And so checks the =
    meets the first condition? NAY
    meets the second condition? NAY, for the "<" that it was necessary for this second possibility was already captured by the first.
  • Nice, the answer is very clear (+1) confirmed the theory, thank you for your time.

  • no problem, if you have problems again can call me via chat @Leonancarvalho

Browser other questions tagged

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