Solved

Use Regular Expressions to Extract Data Fields in VB.NET

Posted on 2009-03-31
2
604 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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

685 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