Solved

How to validate a comma delimited string with Regex

Posted on 2006-06-26
3
482 Views
Last Modified: 2008-03-06
How to use RegEx to validate a string in the format of

12C;325252C;99999C;378C;28C;3535345342C;1C;56896745C

The valid format would be any number of numerical value + "C;" as the delimiter except the last element ends with "C"

Thanks.
0
Comment
Question by:cyn_jiang
3 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16988412
1) That looks like semi-colon delimited text, and not comma-delimited

2) Try this:

Imports System.Text.RegularExpressions

    If ValidateInput("12C;325252C;99999C;378C;28C;3535345342C;1C;56896745C") Then
      MessageBox.Show("Valid")
    End If

  End Sub

  Private Function ValidateInput(ByVal input As String) As Boolean
    Dim pattern As String = "^\d*C$"

    Dim elements As String() = input.Split(";")

    For Each element As String In elements

      If Not Regex.IsMatch(element, pattern) Then
        MessageBox.Show("Element is invalid:  " & element)
        Return False
      End If

    Next element

    Return True

  End Function

Bob
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 250 total points
ID: 16988812
Hi cyn_jiang;

This sample code will do what you want.

Imports System.Text.RegularExpressions

        ' Sample Data
        Dim data As String = "12C;325252C;99999C;378C;28C;3535345342C;1C;56896745C"
        ' Regex pattern to validate data
        Dim pattern As String = "^(\d+C;|\d+C)+$"

        ' Check to see if data is valid
        If Regex.IsMatch(data, pattern) Then
            MessageBox.Show("The data is vaild")
        End If


Fernando
0
 

Author Comment

by:cyn_jiang
ID: 16991933
Sorry, it is semi-colon delimited text, I would allow them to input space in between the elements.

So I modified Fernando's code a little bit.

Imports System.Text.RegularExpressions
 
Dim pattern as String = "^(\d+(C; *)|\d+C)+$"

      If Regex.IsMatch(data, pattern) Then
            MessageBox.Show("The data is vaild")
        End If

Thanks for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

919 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now