Solved

Use Regular Expressions to Extract Data Fields in VB.NET

Posted on 2009-03-31
2
603 Views
Last Modified: 2013-11-07
Dear Experts,

I need to use regular expressions to extracted data fields in a string received from a device attached to a serial port. The strings look like the one below:

+IPR: (),(300,600,1200,2400,4800,9600,19200,38400,57600,115200,230400)

The data I want extracted is the one in braces. Using the sample string above, regex should extract the data as follows:

Field1= "" ' i.e. empty screen because there is no data in the first set of braces ()
Field2 = "300,600,1200,2400,4800,9600,19200,38400,57600,115200,230400" ' i.e. data in the second set of braces

There may be more braces with data in the received strings. The returned data can be in a single keyed list e.g.

DataFields(0)=""
DataFields(1)="300,600,1200,2400,4800,9600,19200,38400,57600,115200,230400"
DataFields(3)=...

Any idea how I can best implement this in VB.NET?
0
Comment
Question by:bmatumbura
2 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
ID: 24027741
Try this function, which returns a Generic list of type string.

Wayne
    Public Shared Function GetFields(ByVal fieldString As String) As List(Of String)
 
        Dim patt As String = "\((.*?)\)"
        Dim strList As New List(Of String)
        Dim mtchs As MatchCollection = Regex.Matches(fieldString, patt)
        For Each m As Match In mtchs
            strList.Add(m.Groups(1).Value)
        Next
        Return strList
 
    End Function

Open in new window

0
 
LVL 11

Author Closing Comment

by:bmatumbura
ID: 31564763
Thanks, works perfect!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question