javascript - Filter logic like in reports -
This type of filter argument: (1 and 2) or 3
can be implemented in Python Is Javascript, any ideas? I did not find anything Google I mean, the user will type the filter in textarea, the grammar of the filter (mb with regex) and the pars filter need to be checked. Example of valid and invalid filter:
1 or 2: valid (1 and 3) or 2: valid (1 or 2 more) 3: invalid 1 and 2 (or 3): valid 1 (2 and 3): invalid
How to do any idea?
AFAIK, since boolean expression can have many nested levels - this is not a regular language, and Thus it would not be possible to make a regex without inputting some obstacles for input.
In Python, I will use the eval
to get it if the syntax is false then the exception will be thrown. As stated in the comments, keep in mind that this is a risky safety-war, so I'll first check the input text to avoid injection. [11]: eval ('1 or 2') outside [11]: 1 in [12]: eval ('1 (2 and 3)') - See some examples:
- ------------------------------------------------- - ----------------------- Typer error traceback (most recent call final) & lt; Ipython-input-12-96e407050456 & gt; In & lt; Module & gt; () ---- & gt; 1 eval ('1 (2 and 3)') & lt; String & gt; ('1 and 2 (or 3)') file in the "& lt; string"; in line 1 1 and 2 (or 3) & lt; Module & gt; () Type: Error: The 'int' object is not capable of calling [13]: ^ Syntax error: Invalid syntax [14]: eval ('(1 and 3) or 2') Out [14]: 3
Comments
Post a Comment