Is the order of evaluation of alternatives in a regular expression always taken into account?

0

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

For example:

  

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

Viraria:

[ {attr: "temperature",op: "==",value:"40"}, {...} ] I solved this problem using .split by passing the regular expression /(==|!=|>=|<=|~=|>|<)/ , but I'm not sure if the regular expression will execute in order, as I have >= and <= I put > and < at the end , so that it was not 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 situations, uses and programming languages?

    
asked by anonymous 24.08.2017 / 20:42

1 answer

1
  

There is some RFC documentation that confirms that the expressions   regulate their options in order in all situations,   programming languages?

Yes, you can find documentation about your problem here:

  

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

What you tested with the expressions A and B , is correct and is  reproduced on all Regex flavors, what changes in them are the  your tokens, operators, and modifiers . So I can confirm ( you've just proved ) that: A regex with the OR operator ( | ) checks its possibilities from left to right and as soon as satisfied, it no longer verifies that character, follows the analysis from the next

That's why even though you have the possibility to be found both "

24.08.2017 / 21:26