I need a regex expresssion which match below strings in java.
select * from test
select * from test where a=100
select * from test where a=100 and b=100
the select statement may or may not contain where condition, also it may contain 1 to n number of conditions, i need a single regex to match all of those, can anyone help
i tried to match the string by using below regex expression, but it fails
select (([A-Za-z0-9]{1,20}(|\\,))*|\\*) from ([A-Za-z0-9]{1,20})( where ([A-Za-z0-9]{1,20}((| )(\\=|\\>|\\<)(| ))[A-Za-z0-9]{1,20})|$)
Criteria:
i) a string should start with select
ii) if where is present it should be followed by a pair of variable and value
like below:
`select * from test where a=10;`
iii) Between two variable and value pair and should be present
like below:
`select * from test where a=10 and b=10`
iv) a string may or may not contain where
v) a string should not end with and
public class Test {
public static Pattern pattern;
public static Matcher matcher;
private static final String PATTERN="(\\s)*select(\\s)+((\\*)|([a-zA-Z0-9]+))(\\s)+from(\\s)+[a-zA-Z0-9]*(\\s)+where(\\s)+([a-zA-Z0-9]*(\\s)*=(\\s)*[0-9]*)+(\\s)*((\\s)*and(\\s)*([a-zA-Z0-9]*(\\s)*=(\\s)*[0-9]*))*(\\s)*";
public static void main(String[] args)throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String data;
pattern=Pattern.compile(PATTERN,Pattern.CASE_INSENSITIVE);
do{
data=br.readLine();
matcher=pattern.matcher(data);
if(matcher.find()){
System.out.print("True");
}else{
System.out.print("false");
}
System.out.print(":"+data+":\n");
}while(data!="exit");
}
}
Test Cases(should show true)
select a from test
select a from test where a=10
select a from test where a=10 and b=10
select a from test where a=10 and b=10 and c=10
Test Cases(should show false)
select a from test where
select a from test where a=10 and
select a from test where a=10 b=10
select a from test where a=10 and b=10 and
thanks