tags:

views:

105

answers:

3

This is a followup to an earlier question (http://stackoverflow.com/questions/814042/help-refining-regex-b-d6-1-15-d7-b). The goal is to get the Lat/Lon from the file. This is denoted by an L or the pattern of 6d/7d. I incorrectly stated that there could not be an alpha between start of the Lat (305455) and the Lon (1025446). When I implemented the proposed Regex everything was great till I hit this file. Notice that '6770G37 contains an alpha and broke the pattern or that it had 6 digits and restarted the pattern. Not good enough with Reg Ex to figure it out. Here are three different patterns that I need to find. Thanks!

I  FST E  030                                                       66686500    
    L  305455                                              '6770G37 66686501    
       1025446                                             '6770G37 66686502    
    O  ZCA/999                                                      66686503    
    H  05                                                           66686504


I  ARA                                    '* ACADIANA  AIRPORT         29865996    
            L    300216                               '2425     29865997    
                 0915302                              '2425     29865998    
            S    MSY                                            29865999


I JENNA078033 ' ZFW L 322823 0923754 ' ZFW
+1  A: 

See if this one works.. L\s*(\d{6})\s*(?:'.*\n)?\s*(\d{7})

string sText = @" ..." // all the different patterns you posted. I think I have 4
Regex regex = new Regex(@"L\s*(\d{6})\s*(?:'.*\n)?\s*(\d{7})", RegexOptions.Multiline);
foreach (Match everyMatch in regex.Matches(sText))
{
  Console.WriteLine("L {0}, {1}", everyMatch.Groups[1], everyMatch.Groups[2]);
}

Outputs:

L 305455, 1025446
L 300216, 0915302
L 322823, 0923754
L 322441, 0994055
Gishu
Earlier returns are very positive. Will be later in the day before I know if it nails all of it. Thanks!
glad to help...
Gishu
Gishu, close to the right solution, but missed on this configuration. I will post it as an answer so the formatting is correct. Thanks again.
A: 

This is a followup to Gishu's answer, but could not get the comments area to format well enough. The Regex he proposed L\s*(\d{6})\s*(?:'.*\n)?\s*(\d{7}) worked for several of the possibilities, but failed on this pattern using the .Net engine with multi-line option turned on.

The goal is to capture the Lat 322441 and the Lon 0994055.

Returned Match:

L  322441                                             '1325     66685780 

Sample data.....

I  ABI E   018                                                          66685779    
        L  322441                                             '1325     66685780    
           0994055                                            '1325     66685781    
        O  ZCF/999                                                      66685782    
        H  05                                                           66685783    

Thanks again guys!

You might want to edit the question to post this, rather than posting it as an answer.
Brian Campbell
Expresso - my preferred regex tool captures this one correctly with no changes to the regex. Let me see if I can code up a snippet in c#
Gishu
A: 

You might try aiding the matching by enforcing legal lat/lon values. 0994055 appears to mean 99 40' 55". If this is the case, that means your first digit is always going to be 1 or 0 (I thought it might go from 0-360 since there's no sign indicated, but there's no sign for lat either, and that appears to be limited to 6 chars, which would indicate sign is either stored elsewhere or not at all).

So instead of (\d{7}) perhaps try ([01]\d{6})

I also notice a pattern with the '###### following lat and lon, it's always the same. So maybe try using a backreference as well:

L\s*(\d{6})\s*(('\d*)\s*.*\n)?\s*(\d{7}(\s*\3)?)

You might also try creating smaller regexes specific to each general case (multiline, singleline, etc), separating them with an or |. The regex will be a bit uglier, but it might be easier to get to a solution.

Good luck!

patjbs