Question

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

Was it helpful?

Solution 2

Finally i found solution for my question. this will match all my criteria.

select\s(([A-Za-z0-9]{1,20})(|\,[A-Za-z0-9]{1,20})+|\*)\sfrom\s([A-Za-z0-9]{1,20})(\swhere\s[A-Za-z0-9]{1,20}\=[A-Za-z0-9]{1,20}($|(\sand\s[A-Za-z0-9]{1,20}\=[A-Za-z0-9]{1,20})+$)|$)

Hope it will help anyone in future.

OTHER TIPS

I am new to Regular expressions. After reading this question i tried the following in online regex testers. it works fine.

(\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)+and(\s)+([a-zA-Z0-9]*(\s)*=(\s)*[0-9]*))*(\s)*

I think it is too long. But it works fine for case 2 and case 3. If you want to achieve only these cases through regex , you need two regex to achieve your desired solution.

If you want to achieve all the three in single regex its not possible[According to my knowledge] .
As Bobbel mentioned in comments , Its not possible.

Because you need to check whether "where" is present or not . If present , it should have condition. Else "where" itself shouldn't be present.

EDIT

import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class HelloWorld
{
  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)+and(\\s)+([a-zA-Z0-9]*(\\s)*=(\\s)*[0-9]*))*(\\s)*";

 public static void main(String []args)
 {
    System.out.println("Hello World");
    String data="select * from asdf where a  = 20 and ax =20";        
    Pattern pattern=Pattern.compile(PATTERN); 
    Matcher matcher=pattern.matcher(data);
    if(matcher.find())
    {
      System.out.print("True");
    }
    else
    {
      System.out.print("false");
    }
    System.out.print(":"+data+":\n");
 }

}

See screenshots

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top