Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

REGEX help needed - parsing complex SQL statements

794256Dec 15 2008 — edited Dec 15 2008
I guess "complex" might be in the eye of the beholder, but for me, this is sort of complex.

I've written a small java app that parses some SQL statements. I need to find SQL statements that have nested AND and/or OR predicates 3 or more levels deep in parenthesis.

The REGEX I've come up with works somewhat, but it is too broad and finds too many statements that are not applicable to my needs.

I've left two test statements uncommented - they pretty much sum up what I need to find and don't want to find. The Regex, however, matches both.

import java.util.regex.* ; 

	
public class SQL_Regex {


	public static void main(String[] args) {
			
			
						
		// Find AND/OR predicates nested at least 3 levels deep 
			
		String pat = new String(".+\\bwhere\\b.*\\(.*\\(.*\\(.+(\\bAND\\b|\\bOR\\b).+\\).*\\).*\\)"); 
		
		Matcher matcher = null ; 
		Pattern pattern = null ; 
		try { 
			pattern = Pattern.compile(pat, Pattern.CASE_INSENSITIVE ) ; 
		}
		catch (Exception e) { 
			System.out.println("Cannot compile REGEX pattern.  Exiting") ; 
			System.exit(-1) ; 
		}
		
		String sql[] = new String[] { 
			//"select * from table where (col1 = 1 and (col2 = 2 or (col3 = 3 or col4 = 4) ) )",  
			//"select * from table where ((( col1 = 1 and not col2 = 2)and(col3 = 3))or col4 = 4)" , 
			//"select * from table where (( col3 = 3 or col4 = 4))" ,  
			//"select * from table where  (col1 = col2)  and  (col3 = 3 or col4 = 4 )" ,  
			"select * from table where  (((col1 = col2)))  and  (((col3 = 3 )))" ,  // Do not want 
			"select * from table where (( col1 = 1 and not (col2 = 2 and(col3 = 3)))or col4 = 4)" } ;  // I want this
			


		for (String statement: sql) {    

			matcher = pattern.matcher(statement) ; 

			if (matcher.find()) { 
				System.out.println("Match YES: " + statement ) ; 
			}
			else { 
				System.out.println("Match NO : " + statement ) ; 
			}
			
		}
	}
}
Edited by: toddburch on Dec 15, 2008 3:30 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 12 2009
Added on Dec 15 2008
3 comments
1,104 views