Search code examples
sqlvb.netregextext-parsing

How to create a regular expression to parse my SQL statement


I want to extract

FROM codes WHERE FieldName='ContactMethod' and IsNull(Deactived,'') != 'T'

from

SELECT FieldDescription,FieldValue FROM codes WHERE FieldName='ContactMethod'
   and IsNull(Deactived,'') != 'T' order by fielddescription

using a regular expression. I have a regex like this:

\FROM.*\order

which extracts

FROM codes WHERE FieldName='ContactMethod' and IsNull(Deactived,'') != 'T' order

Also, how can I get rid of the capitalization?


Solution

  • The trick here would probably be to capture the part you actually want with parens:

    (FROM.*) order
    

    This would greedily match until the last order, if you want only until the first occurrence, match lazily:

    (FROM.*?) order