We help IT Professionals succeed at work.

Help needed with regular expression

andieje
andieje asked
on
271 Views
Last Modified: 2013-11-05
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
Comment
Watch Question

margajet24IT Business Analyst

Commented:
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

Retired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
wonderful as ever fernando
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Glad I was able to help.  ;=)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.