Click here to Skip to main content
15,891,744 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
It seems as if it ought to be simple, but I've spent 4 hours reading the Espresso tutorial and trying various expresions without success. Regex just seems to be a language I don't understand

Need to document a LARGE number of SQL Select statements where data from one system is being SELECTed and INSERTed into another. In many cases the lines are of the form

columnX as columnY

and in other cases they are

CASE WHEN...as columnZ

So, the word "AS" is common in the lines and I need to show the map columnX to columnY or [expression] to columnZ by separately capturing the word(s) before the AS and the word after the AS
Posted

Figured it out: (.*)(?:\bas\b)(.*)
 
Share this answer
 
Comments
TheyCallMeMrJames 3-Sep-10 10:02am    
good work, mate. cheers.
Regex is sexy. It's also a time-pig as you can get to invest a lot of time in an otherwise trivial task. I've gotten lost trying to code some really beautiful things in regex.

That said, why not try this? Read all lines of the file into a string array, then do something like this:

C#
string[] lines = File.ReadAllLines("yourfile");
var mappingLines = from l in lines
                   where l.Contains(" as ")
                   select l;
foreach (var line in mappingLines)
{
    string[] map = line.Split(" as ".ToCharArray());
}


The first bit filters you down to only the lines with " as " in them, giving you your maps. The second bit is just a simple string split.

You might need to work a little on the map after the split to pull out the CASE WHEN, but that would get you started. I'd need more info on that text pattern to help more.

Cheers.
 
Share this answer
 
v2
Comments
Duke Carey 3-Sep-10 10:15am    
Thanks MrJames. I was near to giving up and going down a path like you suggest, simply out of frustration with regex. It keeps nagging at me, though, that regex is too powerful for me to ignore, that it's something I just MUST figure out
TheyCallMeMrJames 3-Sep-10 10:53am    
No worries, I find at times that LINQ + Regex can be very powerful for text processing. I keep this handy: http://regexlib.com/CheatSheet.aspx
Dalek Dave 3-Sep-10 11:17am    
Good Call.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900