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?
– Paz
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
– LeonanCarvalho
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.
– Paz
I edited the question to clarify my doubt. And I changed the references in the snippet.
– LeonanCarvalho
Check if this is what you wanted, I hope to have helped :D
– Paz