?
Solved

How to validate a comma delimited string with Regex

Posted on 2006-06-26
3
Medium Priority
?
491 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 64

Accepted Solution

by:
Fernando Soto earned 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

719 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