Solved

How to validate a comma delimited string with Regex

Posted on 2006-06-26
3
481 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

15 Experts available now in Live!

Get 1:1 Help Now