需要正则表达式来匹配整数或带引号的字符串

时间:2016-01-07 04:25:35

标签: regex

我正在试图找出一个正则表达式来验证其中一个(<,>,=,!=)以确保它是整数或单引号字符串(可能带空格)

一些例子:

SELECT * WHERE field1=5      // OK
SELECT * WHERE field1=   5   // OK
SELECT * WHERE field1<   -2  // OK
SELECT * WHERE field1='5'    // OK
SELECT * WHERE field1=a      // NOT OK - a is not an integer
SELECT * WHERE field1!='a'   // OK
SELECT * WHERE field1='a     // NOT OK - missing closing quote

EDIT:  I forgot to add it should work with multiple fields such as
SELECT * WHERE field1=5 AND field2=b  // NOT OK since field2 following is not a int
SELECT * WHERE field1=5 AND (field1<6 AND fieldb='a')  // OK 

1 个答案:

答案 0 :(得分:2)

.*(=|<|>|!=)\s*(-?\d+|'[^']+')

regex101