Link to home
Start Free TrialLog in
Avatar of andieje
andieje

asked on

Help needed with regular expression

Hi

I would appreciate some help/feedback/improvements on some regular expression stuff i am doing. Here is the string i need to parse. It's hopefully quite easy

3480:#ID,357262002205552,IOP,O,0x00,I,0x00,GPSEX,A,D,170708,T,145937,La,51.52225,N,Lo,0.11623,E,V,0.5

This consists of
1. 4 digit number followed by :#ID,
2. 15 digit number which i need to extract following by the constant text ,IOP,O,0X00,
3. The letter I followed by either 0x00 or 0x10. I need to extract the 0x00/0x10
4. GPSEX followed by an A or V. I need to extract the A/V
5. D followed by the date. I need to obtain the date
6. T followed by the time. I need to obtain the time
7. La followed by some numbers then then initial N. I need to extract the numbers
8.  Lo followed by some numbers then then initial E. I need to extract the numbers
9 V followed by some numbers. I need to extract the numbers.

Here is my reg ex/basic code

 Dim gpsRegEx As String = "^\d{4}:#ID,(\d{15}),IOP,O,0x00,I,([0x00|0x10]),GPSEX,([A|V]),D,(\d{6}),T,(\d{6}),La,(*{6,7}),N,Lo,(*{6,7}),E,V,(*{3,4})
 Dim m As Match
 Dim r As Regex = New Regex(gpsRegEx)
m = r.Match(data)

As said earlier, any feedback or improvements much welcomed

thanks
andrea
Avatar of margajet24
margajet24
Flag of Singapore image

please try this
Imports System.Text.RegularExpressions
 
Module Module1
 
    Sub Main()
        Dim pattern As String = "^\d{4}\:\#ID,\d{15},IOP,O,0x00,I,(?<val1>0x[01]0),GPSEX,(?<val2>[AV]),D,(?<date>\d{6}),T,(?<time>\d{6}),La,(?<num1>\d+\.?\d+),N,Lo,(?<num2>\d+\.?\d+),E,V,(?<num3>\d+\.?\d+)$"
 
        Dim input As String = "3480:#ID,357262002205552,IOP,O,0x00,I,0x00,GPSEX,A,D,170708,T,145937,La,51.52225,N,Lo,0.11623,E,V,0.5"
 
        Dim regex As Regex = New Regex(pattern)
 
        Dim match As Match = regex.Match(input)
 
        Console.WriteLine("{0}", match.Groups("val1"))
        Console.WriteLine("{0}", match.Groups("val2"))
        Console.WriteLine("{0}", match.Groups("date"))
        Console.WriteLine("{0}", match.Groups("time"))
        Console.WriteLine("{0}", match.Groups("num1"))
        Console.WriteLine("{0}", match.Groups("num2"))
        Console.WriteLine("{0}", match.Groups("num3"))
 
        Console.ReadLine()
    End Sub
 
End Module

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of andieje
andieje

ASKER

wonderful as ever fernando
Glad I was able to help.  ;=)