关系运算符和引用值的正则表达式

时间:2022-12-06 20:29:26

I have an sql query. I want to allow user to modify only relational operators or values within single quotes

我有一个SQL查询。我想允许用户仅修改单引号内的关系运算符或值

My input string is.

我的输入字符串是。

Select * from defect where Quantity < '9' and Date <= curdate() and Date >=
date_sub(curdate(), interval '3' month)

I am trying following patterns to match the above string as it is. I also tried ^ at beginning and $ at end. But no positive result in any case

我正在尝试按照模式匹配上面的字符串。我也开始尝试^和结束时的$。但无论如何都没有积极的结果

1. Select * from defect where Quantity [<|(>)|(=)|(<=)|(>=)|(like)] '.*' and
Date [<|(>)|(=)|(<=)|(>=)|(!=)|(like)] curdate() and Date [<|(>)|(=)|(<=)|(>=)
|(!=)|(like)] date_sub(curdate(), interval '.*' month)

2. Select * from defect where Quantity (<|(>)|(=)|(<=)|(>=)|(like)) '.*' and
Date (<|(>)|(=)|(<=)|(>=)|(!=)|(like)) curdate() and Date (<|(>)|(=)|(<=)|(>=)
|(!=)|(like)) date_sub(curdate(), interval '.*' month)

Update Need guidance why my patterns are not matching my input string. What could be the mistake?

更新需要指导为什么我的模式与我的输入字符串不匹配。可能是什么错误?

2 个解决方案

#1


2  

There are more problems: You're using square brackets incorrectly:

还有更多问题:您使用的方括号不正确:

[<|(>)|(=)|(<=)|(>=)|(like)]

means "one character out of the following: <>()|=eikl".

表示“以下一个字符:<>()| = eikl”。

Use parentheses instead of brackets, and group sensibly:

使用括号而不是括号,并合理地分组:

([<>=]|[<>]=|like)

Also, you should be more specific: Use '[^']*' instead of '.*'.

此外,您应该更具体:使用'[^'] *'而不是'。*'。

Finally, if you want to match a literal parenthesis, you need to escape it in your regex:

最后,如果要匹配文字括号,则需要在正则表达式中将其转义:

date_sub\(curdate\(\), interval '[^']*' month\)

#2


1  

I found the solution

我找到了解决方案

My pattern 1 was alright just I had to put a backslash before '*' as it has special meanings in Regex. Backslash makes it a normal character

我的模式1没问题,只是我必须在'*'之前加一个反斜杠,因为它在Regex中有特殊的含义。反斜杠使其成为普通角色

Select \* from defect where Quantity [<|(>)|(=)|(<=)|(>=)|(like)] '.*' and
Date [<|(>)|(=)|(<=)|(>=)|(!=)|(like)] curdate() and Date [<|(>)|(=)|(<=)|(>=)
|(!=)|(like)] date_sub(curdate(), interval '.*' month)

It was an easy question. I knew that rule already, but could sort it out after hours.

这是一个简单的问题。我已经知道了这条规则,但可以在下班后解决。

Correction Replaced square brackets[] with parenthesis() and backslash is put before constant parenthesis \( and \)

更正用括号()替换方括号[],并在常量括号\(和\)之前放置反斜杠

Select \* from defect where Quantity (<|(>)|(=)|(<=)|(>=)|(like)) '.*' and
Date (<|(>)|(=)|(<=)|(>=)|(!=)|(like)) curdate\(\) and Date (<|(>)|(=)|(<=)|(>=)
|(!=)|(like)) date_sub\(curdate\(\), interval '.*' month\)

#1


2  

There are more problems: You're using square brackets incorrectly:

还有更多问题:您使用的方括号不正确:

[<|(>)|(=)|(<=)|(>=)|(like)]

means "one character out of the following: <>()|=eikl".

表示“以下一个字符:<>()| = eikl”。

Use parentheses instead of brackets, and group sensibly:

使用括号而不是括号,并合理地分组:

([<>=]|[<>]=|like)

Also, you should be more specific: Use '[^']*' instead of '.*'.

此外,您应该更具体:使用'[^'] *'而不是'。*'。

Finally, if you want to match a literal parenthesis, you need to escape it in your regex:

最后,如果要匹配文字括号,则需要在正则表达式中将其转义:

date_sub\(curdate\(\), interval '[^']*' month\)

#2


1  

I found the solution

我找到了解决方案

My pattern 1 was alright just I had to put a backslash before '*' as it has special meanings in Regex. Backslash makes it a normal character

我的模式1没问题,只是我必须在'*'之前加一个反斜杠,因为它在Regex中有特殊的含义。反斜杠使其成为普通角色

Select \* from defect where Quantity [<|(>)|(=)|(<=)|(>=)|(like)] '.*' and
Date [<|(>)|(=)|(<=)|(>=)|(!=)|(like)] curdate() and Date [<|(>)|(=)|(<=)|(>=)
|(!=)|(like)] date_sub(curdate(), interval '.*' month)

It was an easy question. I knew that rule already, but could sort it out after hours.

这是一个简单的问题。我已经知道了这条规则,但可以在下班后解决。

Correction Replaced square brackets[] with parenthesis() and backslash is put before constant parenthesis \( and \)

更正用括号()替换方括号[],并在常量括号\(和\)之前放置反斜杠

Select \* from defect where Quantity (<|(>)|(=)|(<=)|(>=)|(like)) '.*' and
Date (<|(>)|(=)|(<=)|(>=)|(!=)|(like)) curdate\(\) and Date (<|(>)|(=)|(<=)|(>=)
|(!=)|(like)) date_sub\(curdate\(\), interval '.*' month\)