Skip to main content
deleted 4 characters in body
Source Link
Wiktor Stribiżew
  • 630.9k
  • 41
  • 502
  • 632

You can change the code to

string input = "select * from table where x = 5 and abc = 'p' or def = 1 order by col"; Match match = Regex.Match(input, @"(?i)select@"select.*from [a-z]+ where(.*?)(?=\s+and|$)", RegexOptions.IgnoreCase); 

and it will only capture the where clause up to the next and or the end of the query.

You can change the code to

string input = "select * from table where x = 5 and abc = 'p' or def = 1 order by col"; Match match = Regex.Match(input, @"(?i)select.*from [a-z]+ where(.*?)(?=\s+and|$)", RegexOptions.IgnoreCase); 

and it will only capture the where clause up to the next and or the end of the query.

You can change the code to

string input = "select * from table where x = 5 and abc = 'p' or def = 1 order by col"; Match match = Regex.Match(input, @"select.*from [a-z]+ where(.*?)(?=\s+and|$)", RegexOptions.IgnoreCase); 

and it will only capture the where clause up to the next and or the end of the query.

Source Link
Wiktor Stribiżew
  • 630.9k
  • 41
  • 502
  • 632

You can change the code to

string input = "select * from table where x = 5 and abc = 'p' or def = 1 order by col"; Match match = Regex.Match(input, @"(?i)select.*from [a-z]+ where(.*?)(?=\s+and|$)", RegexOptions.IgnoreCase); 

and it will only capture the where clause up to the next and or the end of the query.