1

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?

1
  • what "flavor" of regex are you using, in what environment? ie, Perl, .NET, etc. Commented Feb 26, 2009 at 15:50

4 Answers 4

1

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
Sign up to request clarification or add additional context in comments.

8 Comments

This one works but I don't want the word "Order" i want everything starting from the word "FROM" till the word "order" excluding the word "Order" also i want it case insensitive.
isn't that what the (parentheses) are for?
It still gives me " FROM codes WHERE FieldName='ContactMethod' and IsNull(Deactived,'') != 'T' order"
The first match (the stuff in brackets) will be the part without order. Read up about RegEx matches.
To not have order show up in the match, do this "(FROM.*)(?=order)"
|
1

Expanding on Fabian Steeg's answer

 Dim regex As Regex = New Regex( _
          "(FROM.*?) ORDER", _
        RegexOptions.IgnoreCase _
        Or RegexOptions.CultureInvariant _
        Or RegexOptions.IgnorePatternWhitespace _
        Or RegexOptions.Compiled _
        )

    Dim ms As MatchCollection = regex.Matches(InputText)

where InputText is of course your SQL query string.

ms(1) should hold the parentheses match

Comments

0

If it comes down to it you can ignore capitalization by doing (F|f)(R|r)(O|o)(M|m).

2 Comments

yup..that solves the case sensitive issue. I am still getting the word "Order" which i don't want. I am using (from|FROM.*)order|ORDER
Nearly all regex flavors support a case-insensitivity flag.
0

Interactive tools like RegexBuddy ($40) or The Regex Coach (free) would really help you to design and debug regular expressions for most platforms.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.