Skip to main content
added 55 characters in body
Source Link
user1737619
  • 247
  • 1
  • 3
  • 15

I'm trying to parse an sql where I would like to get the where clause of the statement.

Below is the piece of code I have written:

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(.*)(?:order by .*)?",RegexOptions.IgnoreCase); 

But here the output I get includes the order by statement which I dont want. I get the expected output if I removed last '?', but the input statement might or might not contain order by.

Expected Output: " x = 5 and abc = 'p' or def = 1 "

can you please correct my regex

I'm trying to parse an sql where I would like to get the where clause of the statement.

Below is the piece of code I have written:

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(.*)(?:order by .*)?",RegexOptions.IgnoreCase); 

But here the output I get includes the order by statement which I dont want. I get the expected output if I removed last '?', but the input statement might or might not contain order by.

can you please correct my regex

I'm trying to parse an sql where I would like to get the where clause of the statement.

Below is the piece of code I have written:

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(.*)(?:order by .*)?",RegexOptions.IgnoreCase); 

But here the output I get includes the order by statement which I dont want. I get the expected output if I removed last '?', but the input statement might or might not contain order by.

Expected Output: " x = 5 and abc = 'p' or def = 1 "

can you please correct my regex

Source Link
user1737619
  • 247
  • 1
  • 3
  • 15

Regular Expression + C#

I'm trying to parse an sql where I would like to get the where clause of the statement.

Below is the piece of code I have written:

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(.*)(?:order by .*)?",RegexOptions.IgnoreCase); 

But here the output I get includes the order by statement which I dont want. I get the expected output if I removed last '?', but the input statement might or might not contain order by.

can you please correct my regex